import { timeout } 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 });
return await response.json();
},
[timeout({ waitTime: TimeSpan.fromSeconds(2) })],
{
signalBinder: {
getSignal: (args) => args[1],
forwardSignal: (args, signal) => {
args[1] = signal;
}
}
}
)
.invoke("URL");
The
timeout
middleware 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
Promise
will be aborted. To ensure correct abortion behavior, provide anAbortSignalBinder
toAsyncHooks
.IMPORT_PATH:
"@daiso-tech/core/async"