]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - shared/core-utils/common/promises.ts
Type isPromise
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / promises.ts
... / ...
CommitLineData
1function isPromise <T = unknown> (value: T | Promise<T>): value is Promise<T> {
2 return value && typeof (value as Promise<T>).then === 'function'
3}
4
5function isCatchable (value: any) {
6 return value && typeof value.catch === 'function'
7}
8
9function 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
21export {
22 isPromise,
23 isCatchable,
24 timeoutPromise
25}