(promise: Promise<T>, sec: number, errMsg = 'timeout')
| 9 | } |
| 10 | |
| 11 | export async function timeout<T>(promise: Promise<T>, sec: number, errMsg = 'timeout'): Promise<T> { |
| 12 | // so we can have a more comprehensive error stack |
| 13 | const err = new Error(errMsg); |
| 14 | let timeout: NodeJS.Timeout; |
| 15 | return Promise.race([ |
| 16 | promise.then( |
| 17 | (r) => { |
| 18 | clearTimeout(timeout); |
| 19 | return r; |
| 20 | }, |
| 21 | (e) => { |
| 22 | clearTimeout(timeout); |
| 23 | throw e; |
| 24 | } |
| 25 | ), |
| 26 | new Promise<never>((resolve, reject) => { |
| 27 | timeout = setTimeout(() => reject(err), sec * 1000); |
| 28 | }), |
| 29 | ]); |
| 30 | } |
| 31 | |
| 32 | export class BackoffError extends Error { |
| 33 | readonly lastError: any; |
no outgoing calls
no test coverage detected