PeerTube/shared/core-utils/common/promises.ts

26 lines
602 B
TypeScript
Raw Normal View History

2023-01-24 04:57:25 -06:00
function isPromise <T = unknown> (value: T | Promise<T>): value is Promise<T> {
return value && typeof (value as Promise<T>).then === 'function'
2021-07-26 08:04:37 -05:00
}
function isCatchable (value: any) {
return value && typeof value.catch === 'function'
}
2022-08-08 03:42:08 -05:00
function timeoutPromise <T> (promise: Promise<T>, timeoutMs: number) {
let timer: ReturnType<typeof setTimeout>
return Promise.race([
promise,
new Promise((_res, rej) => {
timer = setTimeout(() => rej(new Error('Timeout')), timeoutMs)
})
]).finally(() => clearTimeout(timer))
}
2021-07-26 08:04:37 -05:00
export {
isPromise,
2022-08-08 03:42:08 -05:00
isCatchable,
timeoutPromise
2021-07-26 08:04:37 -05:00
}