UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

40 lines (39 loc) 1.56 kB
/** * @module Async */ import { type AsyncMiddlewareFn, type HookContext } from "../../../utilities/_module-exports.js"; import type { TimeoutSettings } from "../../../async/middlewares/timeout/timeout.type.js"; /** * 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 an {@link AbortSignalBinder | `AbortSignalBinder`} to {@link AsyncHooks | `AsyncHooks`}. * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group Middlewares * @throws {TimeoutAsyncError} {@link TimeoutAsyncError} * * @example * ```ts * 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"); * ``` */ export declare function timeout<TParameters extends unknown[], TReturn, TContext extends HookContext>(settings?: NoInfer<TimeoutSettings<TParameters, TContext>>): AsyncMiddlewareFn<TParameters, TReturn, TContext>;