p-defer/index.d.ts

40 lines
808 B
TypeScript
Raw Permalink Normal View History

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-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;
}
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>;