From 2eb6954d7bc5bb0e88a3b20aa62db7b8b08395d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 25 Apr 2023 00:41:00 +0200 Subject: [PATCH] refactor(ui): sync promiseWithTimeout() helper MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- ui/web/src/composables/Utils.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ui/web/src/composables/Utils.ts b/ui/web/src/composables/Utils.ts index 8ce3b6ec..8cb957ab 100644 --- a/ui/web/src/composables/Utils.ts +++ b/ui/web/src/composables/Utils.ts @@ -1,3 +1,5 @@ +import util from 'node:util'; + export default class Utils { // STATE public static isUndefined(value: unknown): boolean { @@ -20,6 +22,10 @@ export default class Utils { // if (this.isIterable(obj) === false) cb(); // } + public static isPromisePending(promise: Promise): boolean { + return util.inspect(promise).includes('pending'); + } + public static async promiseWithTimeout( promise: Promise, timeoutMs: number, @@ -31,7 +37,10 @@ export default class Utils { // Create a timeout promise that rejects in timeout milliseconds const timeoutPromise = new Promise((_, reject) => { setTimeout(() => { - timeoutCallback(); + if (Utils.isPromisePending(promise)) { + timeoutCallback(); + // FIXME: The original promise shall be canceled + } reject(timeoutError); }, timeoutMs); }); -- 2.34.1