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");
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"