When creating a Promise
in Javascript, it can be helpful to resolve
or reject
that promise from outside of the created promise's scope. However, Javascript's built-in Promise
API does not provide a native solution.
Deferred
is meant to allow developers to freely create promises and then resolve
or reject
them from anywhere in their code, as long as they have a reference to the created Deferred
object.
import { Deferred } from '@unifire-js/async';
async function test() {
// Create a deferred promise
const deferred = new Deferred();
// After 3 seconds, resolve the deferred promise with the value, "Test"
setTimeout(() => {
deferred.resolve('Test');
}, 3000);
// Obtain the result of deferred once it has resolved
const deferredResult = await deferred.promise;
console.log(deferredResult); // 'Test'
}
Argument |
Type |
Description |
None |
None |
None |
Property |
Type |
Description |
promise |
Promise |
The native Javascript promise stored by Deferred . |
settled |
boolean |
Flag indicating whether the internal promise has resolved. |
Method |
Return |
Description |
resolve(*) |
null |
The native Javascript promise.resolve function used to resolve the native promise stored by Deferred . |
reject(*) |
null |
The native Javascript promise.reject function used to reject the native promise stored by Deferred . |
Method |
Return |
Description |
resolve(*) |
Deferred |
Creates a new Deferred instance, instantly resolves it, and returns that new instance. The Deferred equivalent of Promise.resolve() . |
reject(*) |
Deferred |
Creates a new Deferred instance, instantly rejects it, and returns that new instance. The Deferred equivalent of Promise.reject() . |