]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/core-utils/common/promises.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / promises.ts
index 7ef9d60b6a80948e0cbc5ebd4554961d547265c5..dc0db9074825594d3298e0c48b376b6dd16acc4d 100644 (file)
@@ -6,7 +6,20 @@ 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
+  isCatchable,
+  timeoutPromise
 }