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