
The DeferredPromise class is built on top of the native Promise class. It provides a way to resolve or reject a promise from the outside.
The resolve method is used to signal that the DeferredPromise has completed successfully and to provide the resulting value.
import { DeferredPromise } from '@aracna/core';
(async () => {
let promise;
promise = new DeferredPromise();
console.log('promise created', Date.now());
setTimeout(() => promise.resolve(), 1000);
await promise.instance;
console.log('promise resolved', Date.now());
})();The reject method is used to signal that the DeferredPromise has been rejected and to provide the reason for the rejection.
import { DeferredPromise } from '@aracna/core';
(async () => {
let promise;
promise = new DeferredPromise();
console.log('promise created', Date.now());
setTimeout(() => promise.reject(), 1000);
try {
await promise.instance;
} catch (error) {
console.log('promise rejected', error);
}
})();