A Promise that can be resolved externally
yarn add @woubuc/deferred
The library is written in Typescript to types are included.
importDeferredfrom'@woubuc/deferred';// Use it as a regular promiseletpromise=newDeferred((resolve,reject)=>{resolve('done');});// Or resolve it externallypromise.resolve('done');// Or omit the promise body altogetherletdeferred=newDeferred();deferred.resolve('done');letdeferred=newDeferred<T>(promiseCallback);Creates a new Deferred promise. Takes a (resolve, reject) callback just like a regular promise.
<T>: Type of the promise resolve value
deferred.resolve(value : T);Resolves the promise with value
deferred.reject(error ?: any);Rejects the promise with an optional error.
- Extends the native
Promiseobject, so it should be fully compatible with regular promises - Resolving and rejecting follows the same rules as with regular promises (i.e. a promise can only be settled once)
- Resolving the promise externally will not stop or code inside the constructor callback from executing