From b842c65bd62d35d7b19f5d45371dfe0c6539e6d3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 3 Aug 2024 14:53:24 +0200 Subject: [PATCH] test: add AsyncLock test MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/AsyncLock.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/utils/AsyncLock.ts b/src/utils/AsyncLock.ts index f6b793a6..f69faac8 100644 --- a/src/utils/AsyncLock.ts +++ b/src/utils/AsyncLock.ts @@ -2,7 +2,7 @@ import { Queue } from 'mnemonist' -import { Constants } from './Constants.js' +import { isAsyncFunction } from './Utils.js' export enum AsyncLockType { configuration = 'configuration', @@ -22,11 +22,16 @@ export class AsyncLock { } public static async runExclusive(type: AsyncLockType, fn: () => T | Promise): Promise { - return await AsyncLock.acquire(type) - .then(fn) - .finally(() => { - AsyncLock.release(type).catch(Constants.EMPTY_FUNCTION) - }) + try { + await AsyncLock.acquire(type) + if (isAsyncFunction(fn)) { + return await fn() + } else { + return fn() as T + } + } finally { + await AsyncLock.release(type) + } } private static async acquire (type: AsyncLockType): Promise { -- 2.34.1