Class LazyPromise<TValue>

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

  • defer
  • retryAttempts
  • retryPolicy
  • backoffPolicy
  • abort
  • timeout

The order in which these methods are called does not affect their methodality. Internally, the following execution order is applied:

  1. timeout
  2. retryAttempts
  3. abort

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 { LazyPromise } from "@daiso-tech/core";
import {} from "node:fs/promises";

(async () => {
const promise = new LazyPromise(async () => {
console.log("I am lazy");
});
// "I am lazy" will only logged when awaited or then method i called.
await promise;
})();

Type Parameters

  • TValue

Implements

Constructors

Methods

  • The abort method aborts the LazyPromise by the passed in abortSignal.

    Parameters

    • abortSignal: AbortSignal

    Returns this

    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.

    Parameters

    Returns this

    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 defer method executes the LazyPromise without awaiting it.

    Returns void

    import { LazyPromise, delay, TimeSpan } from "@daiso-tech/core";

    const promise =
    new LazyPromise(async () => {
    await delay(TimeSpan.fromSeconds(1));
    // Will be loged after one second
    console.log("Done !");
    });
    promise.defer();
    // Will be logged immediately
    console.log("Hello");
  • The retryAttempts method is used for setting max retry attempts.

    Parameters

    • attempts: number

    Returns this

    import { LazyPromise } from "@daiso-tech/core";

    const promise =
    new LazyPromise(async () => {
    console.log("A");
    throw new Error("Error occured!");
    })
    .retryAttempts(3)
    (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.

    Parameters

    Returns this

    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.

    Parameters

    Returns this

    import { LazyPromise, delay, TimeSpan } from "@daiso-tech/core";

    const promise =
    new LazyPromise(async () => {
    await delay(TimeSpan.fromMinutes(1));
    })
    .timeout(TimeSpan.fromSeconds(1));
    (async () => {
    // An timeout error will be thrown.
    await promise;
    })();