aboutsummaryrefslogblamecommitdiffhomepage
path: root/shared/core-utils/common/promises.ts
blob: dc0db9074825594d3298e0c48b376b6dd16acc4d (plain) (tree)
1
2
3
4
5
6
7
8







                                                   











                                                                      

            

                
 
function isPromise (value: any) {
  return value && typeof value.then === 'function'
}

function isCatchable (value: any) {
  return value && typeof value.catch === 'function'
}

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))
}

export {
  isPromise,
  isCatchable,
  timeoutPromise
}