From: Jérôme Benoit Date: Mon, 24 Apr 2023 22:41:00 +0000 (+0200) Subject: refactor(ui): sync promiseWithTimeout() helper X-Git-Tag: v1.2.12~86 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2eb6954d7bc5bb0e88a3b20aa62db7b8b08395d1;p=e-mobility-charging-stations-simulator.git refactor(ui): sync promiseWithTimeout() helper Signed-off-by: Jérôme Benoit --- 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); });