The abort method aborts the LazyPromise by the passed in abortSignal.
import { LazyPromise, delay, TimeSpan } from "@daiso-tech/core";
const abortController = new AbortController();
const promise =
new LazyPromise(async () => {
await delay(TimeSpan.fromMinutes(1));
})
.abort(abortController.signal);
(async () => {
setTimeout(() => {
abortController.abort();
}, 1000);
// An timeout error will be thrown.
await promise;
})();
The backoffPolicy method is used for setting a custom BackoffPolicy.
import { LazyPromise, linearBackoffPolicy } from "@daiso-tech/core";
const promise =
new LazyPromise(async () => {
console.log("A");
throw new Error("Error occured!");
})
.retryAttempts(3)
.backoffPolicy(linearBackoffPolicy())
(async () => {
// Will log "A" 3 times and then retry error will be thrown.
await promise;
})();
The backoffPolicy method is used for setting a custom BackoffPolicy.
import { LazyPromise } from "@daiso-tech/core";
class ErrorA extends Error {}
const promise =
new LazyPromise(async () => {
console.log("A");
throw new Error("Error occured!");
})
.retryAttempts(3)
// Will only retry an error that is instance ErrorA
.retryPolicy(error => error instanceof ErrorA)
(async () => {
// Will log "A" 1 time and then error will be thrown.
await promise;
})();
The timeout method aborts the LazyPromise if it exceeds the given time by throwning an error.
Static
allThe all method works similarly to Promise.all with the key distinction that it operates lazily.
Optional
settings: LazyPromiseSettingsStatic
allThe allSettled method works similarly to Promise.allSettled with the key distinction that it operates lazily.
Optional
settings: LazyPromiseSettingsStatic
anyThe any method works similarly to Promise.any with the key distinction that it operates lazily.
Optional
settings: LazyPromiseSettingsStatic
raceThe race method works similarly to Promise.race with the key distinction that it operates lazily.
Optional
settings: LazyPromiseSettingsStatic
wrapThe wrapFn is convience method used for wrapping a async method with a LazyPromise.
Optional
settings: LazyPromiseSettings
The LazyPromise class is used for creating lazy PromiseLike object that will only execute when awaited or when then method is called. The class includes helpful methods
The order in which these methods are called does not affect their methodality. Internally, the following execution order is applied:
This means that combining all methods ensures the retryAttempts method will continue retrying even if the timeout is triggered, while the abort method takes priority to cancel all operations if activated.
Example