aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/core-utils/common
diff options
context:
space:
mode:
Diffstat (limited to 'shared/core-utils/common')
-rw-r--r--shared/core-utils/common/promises.ts15
1 files changed, 14 insertions, 1 deletions
diff --git a/shared/core-utils/common/promises.ts b/shared/core-utils/common/promises.ts
index 7ef9d60b6..dc0db9074 100644
--- a/shared/core-utils/common/promises.ts
+++ b/shared/core-utils/common/promises.ts
@@ -6,7 +6,20 @@ function isCatchable (value: any) {
6 return value && typeof value.catch === 'function' 6 return value && typeof value.catch === 'function'
7} 7}
8 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
9export { 21export {
10 isPromise, 22 isPromise,
11 isCatchable 23 isCatchable,
24 timeoutPromise
12} 25}