From 5a921e7b74910414626bfc9672b857e987e3ebed Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 8 Aug 2022 10:42:08 +0200 Subject: Move to bullmq --- shared/core-utils/common/promises.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'shared/core-utils/common') 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) { return value && typeof value.catch === 'function' } +function timeoutPromise (promise: Promise, timeoutMs: number) { + let timer: ReturnType + + return Promise.race([ + promise, + + new Promise((_res, rej) => { + timer = setTimeout(() => rej(new Error('Timeout')), timeoutMs) + }) + ]).finally(() => clearTimeout(timer)) +} + export { isPromise, - isCatchable + isCatchable, + timeoutPromise } -- cgit v1.2.3