import { timeout } from "@daiso-tech/core/resilience";
import { TimeSpan } from "@daiso-tech/core/time-span";
import { AsyncHooks } from "@daiso-tech/core/hooks";
const data = await new AsyncHooks(
async (url: string, signal?: AbortSignal): Promise<unknown> => {
const response = await fetch(url, { signal });
return await response.json();
},
[timeout({ waitTime: TimeSpan.fromSeconds(2) })],
{
signalBinder: {
getSignal: (args) => args[1],
forwardSignal: (args, signal) => {
args[1] = signal;
}
}
}
)
.invoke("URL");
The
timeoutmiddleware automatically cancels functions after a specified time period, throwing an error when aborted.Note when a timeout occurs, the function call continues executing in the background and only the
Promisewill be aborted. To ensure correct abortion behavior, provide an AbortSignalBinder |AbortSignalBinderto AsyncHooks |AsyncHooks.IMPORT_PATH:
"@daiso-tech/core/resilience"