Function
withTimeout
(
promise: Promise<T>,
timeout: number,
onTimedOut?: () => void,
)
Source from the content-addressed store, hash-verified
| 34 | }); |
| 35 | |
| 36 | export const withTimeout = <T>( |
| 37 | promise: Promise<T>, |
| 38 | timeout: number, |
| 39 | onTimedOut?: () => void, |
| 40 | ): Promise<T> => { |
| 41 | return new Promise((resolve, reject) => { |
| 42 | // Set up the timeout |
| 43 | const timeoutId = setTimeout(() => { |
| 44 | onTimedOut?.(); |
| 45 | reject(new APIConnectionTimeoutError({ message: "Request timed out" })); |
| 46 | }, timeout); |
| 47 | |
| 48 | promise |
| 49 | .then((result) => { |
| 50 | clearTimeout(timeoutId); |
| 51 | resolve(result); |
| 52 | }) |
| 53 | .catch((error) => { |
| 54 | clearTimeout(timeoutId); |
| 55 | reject(error); |
| 56 | }); |
| 57 | }); |
| 58 | }; |
Callers
nothing calls this directly
Tested by
no test coverage detected