2021-04-09 05:21:28 +00:00
|
|
|
export interface DeferredPromise<ValueType> {
|
|
|
|
/**
|
|
|
|
The deferred promise.
|
|
|
|
*/
|
|
|
|
promise: Promise<ValueType>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Resolves the promise with a value or the result of another promise.
|
|
|
|
|
|
|
|
@param value - The value to resolve the promise with.
|
|
|
|
*/
|
|
|
|
resolve(value?: ValueType | PromiseLike<ValueType>): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Reject the promise with a provided reason or error.
|
|
|
|
|
|
|
|
@param reason - The reason or error to reject the promise with.
|
|
|
|
*/
|
|
|
|
reject(reason?: unknown): void;
|
2019-04-03 05:19:13 +00:00
|
|
|
}
|
2019-03-17 12:21:07 +00:00
|
|
|
|
2019-06-07 08:08:42 +00:00
|
|
|
/**
|
|
|
|
Create a deferred promise.
|
|
|
|
|
|
|
|
@example
|
|
|
|
```
|
2021-04-09 05:21:28 +00:00
|
|
|
import pDefer from 'p-defer';
|
2019-06-07 08:08:42 +00:00
|
|
|
|
2021-04-09 05:21:28 +00:00
|
|
|
function delay(milliseconds) {
|
2019-06-07 08:08:42 +00:00
|
|
|
const deferred = pDefer();
|
2021-04-09 05:21:28 +00:00
|
|
|
setTimeout(deferred.resolve, milliseconds, '🦄');
|
2019-06-07 08:08:42 +00:00
|
|
|
return deferred.promise;
|
|
|
|
}
|
2019-04-03 05:19:13 +00:00
|
|
|
|
2021-04-09 05:21:28 +00:00
|
|
|
console.log(await delay(100));
|
|
|
|
//=> '🦄'
|
2019-06-07 08:08:42 +00:00
|
|
|
```
|
|
|
|
*/
|
2021-04-09 05:21:28 +00:00
|
|
|
export default function pDefer<ValueType>(): DeferredPromise<ValueType>;
|