race-cancellation
Version:
Utilities for using Promise.race([task, cancellation]) for async/await code.
20 lines • 868 B
TypeScript
import { CancellableAsyncFn, RaceCancelFn } from "./interfaces.js";
/**
* Wrap a cancellable async function with a timeout.
*
* @remarks
*
* @example
* ```js
* async function fetchWithTimeout(url, timeoutMs, raceCancel) {
* return await withTimeout((raceTimeout) => cancellableFetch(url, raceTimeout), timeoutMs, raceCancel);
* }
* ```
*
* @param cancellableAsync - a {@link CancellableAsyncFn} function
* @param milliseconds - a timeout in miliseconds
* @param raceCancel - an optional outer scope {@link RaceCancelFn} function that will be combined with the timeout race before being passed to the {@link CancellableAsyncFn} function
* @public
*/
export default function withTimeout<TResult>(cancellableAsync: CancellableAsyncFn<TResult>, milliseconds: number, raceCancel?: RaceCancelFn): Promise<TResult>;
//# sourceMappingURL=withTimeout.d.ts.map