Kit
Functions

getAbortablePromise

getAbortablePromise<T>(promise, abortSignal?): Promise<T>

Returns a new promise that will reject if the abort signal fires before the original promise settles. Resolves or rejects with the value of the original promise otherwise.

Type Parameters

Type Parameter
T

Parameters

ParameterType
promisePromise<T>
abortSignal?AbortSignal

Returns

Promise<T>

Example

const result = await getAbortablePromise(
    // Resolves or rejects when `fetch` settles.
    fetch('https://example.com/json').then(r => r.json()),
    // ...unless it takes longer than 5 seconds, after which the `AbortSignal` is triggered.
    AbortSignal.timeout(5000),
);

On this page