• The retry middleware enables automatic retries for all errors or specific errors, with configurable backoff policies. An error will be thrown when all retry attempts fail.

    IMPORT_PATH: "@daiso-tech/core/async"

    Type Parameters

    • TParameters extends unknown[]
    • TReturn
    • TContext extends Partial<Record<string | symbol, unknown>>

    Parameters

    Returns AsyncMiddlewareFn<TParameters, TReturn, TContext>

    import { retry } from "@daiso-tech/core/async";
    import { AsyncHooks, TimeSpan } from "@daiso-tech/core/utilities";

    const data = await new AsyncHooks(
    async (url: string, signal?: AbortSignal): Promise<unknown> => {
    const response = await fetch(url, { signal });
    const json = await response.json();
    if (!response.ok) {
    return json;
    }
    return json;
    },
    [retry()],
    {
    signalBinder: {
    getSignal: (args) => args[1],
    forwardSignal: (args, signal) => {
    args[1] = signal;
    }
    }
    }
    )
    .invoke("URL");

    The middleware works also when the function returns a Result | Result type.

    import { retry } from "@daiso-tech/core/async";
    import { AsyncHooks, TimeSpan, Result, resultFailure, resultSuccess } from "@daiso-tech/core/utilities";

    const data = await new AsyncHooks(
    async (url: string, signal?: AbortSignal): Promise<Result> => {
    const response = await fetch(url, { signal });
    const json = await response.json();
    if (!response.ok) {
    return resultFailure(json);
    }
    return resultSuccess(json);
    },
    [retry()],
    {
    signalBinder: {
    getSignal: (args) => args[1],
    forwardSignal: (args, signal) => {
    args[1] = signal;
    }
    }
    }
    )
    .invoke("URL");