Home > tab-info > guardWithTimeout
guardWithTimeout() function
“Wrap” a promise in a timeout, to ensure that it doesn’t “hang” forever in a perpetually pending state.
Signature:
export declare function guardWithTimeout<T>(promise: PromiseLike<T>, timeout: number): Promise<T>;
Parameters
Parameter | Type | Description |
---|---|---|
promise | PromiseLike<T> | promise to wrap |
timeout | number | timeout in ms |
Returns:
Promise<T>
a new promise, guarded with a timeout
Example
const fetchPromise = fetch('http://example.com');
// Give up and fail after 4.0s
const guarded = guardWithTimeout(fetchPromise, 4000);
guarded.then(data => {
// same data that would have come from `fetch`
})