]>
Commit | Line | Data |
---|---|---|
15a7eafb C |
1 | function isPromise (value: any) { |
2 | return value && typeof value.then === 'function' | |
3 | } | |
4 | ||
5 | function isCatchable (value: any) { | |
6 | return value && typeof value.catch === 'function' | |
7 | } | |
8 | ||
5a921e7b C |
9 | function timeoutPromise <T> (promise: Promise<T>, timeoutMs: number) { |
10 | let timer: ReturnType<typeof setTimeout> | |
11 | ||
12 | return Promise.race([ | |
13 | promise, | |
14 | ||
15 | new Promise((_res, rej) => { | |
16 | timer = setTimeout(() => rej(new Error('Timeout')), timeoutMs) | |
17 | }) | |
18 | ]).finally(() => clearTimeout(timer)) | |
19 | } | |
20 | ||
15a7eafb C |
21 | export { |
22 | isPromise, | |
5a921e7b C |
23 | isCatchable, |
24 | timeoutPromise | |
15a7eafb | 25 | } |