( promiseArg: Promise<T>, timeout: number, )
| 11 | |
| 12 | // https://betterstack.com/community/guides/scaling-nodejs/nodejs-timeouts/ |
| 13 | export async function promiseTimeout<T>( |
| 14 | promiseArg: Promise<T>, |
| 15 | timeout: number, |
| 16 | ): Promise<T> { |
| 17 | let timer: NodeJS.Timeout; |
| 18 | const timeoutPromise = new Promise<never>((_, reject) => { |
| 19 | timer = setTimeout(() => { |
| 20 | reject(new TimeoutError(timeout)); |
| 21 | }, timeout); |
| 22 | }); |
| 23 | |
| 24 | try { |
| 25 | return await Promise.race([ promiseArg, timeoutPromise ]); |
| 26 | } finally { |
| 27 | clearTimeout(timer!); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | export async function runWithTimeout<T>( |
| 32 | scope: () => Promise<T>, |
no outgoing calls
no test coverage detected
searching dependent graphs…