The defer method executes the LazyPromise without awaiting it.
The setAbortSignal 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));
})
.setAbortSignal(abortController.signal);
(async () => {
setTimeout(() => {
abortController.abort();
}, 1000);
// An timeout error will be thrown.
await promise;
})();
The setBackoffPolicy 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!");
})
.setRetryAttempts(3)
.setBackoffPolicy(linearBackoffPolicy())
(async () => {
// Will log "A" 3 times and then retry error will be thrown.
await promise;
})();
The setRetryPolicy 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!");
})
.setRetryAttempts(3)
// Will only retry an error that is instance ErrorA
.setRetryPolicy(error => error instanceof ErrorA)
(async () => {
// Will log "A" 1 time and then error will be thrown.
await promise;
})();
The setTimeout 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.
Static
allThe allSettled method works similarly to Promise.allSettled with the key distinction that it operates lazily.
Static
anyThe any method works similarly to Promise.any with the key distinction that it operates lazily.
Static
raceThe race method works similarly to Promise.race with the key distinction that it operates lazily.
Static
wrapThe wrapFn is convience method used for wrapping a async method with a LazyPromise.
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.
IMPORT_PATH:
"@daiso-tech/core/async"