From 038eddf591810fd6759221fb472cdc6c09cf0f0e Mon Sep 17 00:00:00 2001 From: AkatQuas <295140755@qq.com> Date: Tue, 20 Sep 2022 11:37:52 +0800 Subject: [PATCH] feat: clear timeout when resolving --- src/withTimeout.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/withTimeout.ts b/src/withTimeout.ts index 85b5153..4e75352 100644 --- a/src/withTimeout.ts +++ b/src/withTimeout.ts @@ -75,9 +75,11 @@ export function withTimeout(sync: MutexInterface | SemaphoreInterface, timeout: } return new Promise((resolve, reject) => { - sync.waitForUnlock(weight).then(resolve); - - setTimeout(() => reject(timeoutError), timeout); + const handle = setTimeout(() => reject(timeoutError), timeout); + sync.waitForUnlock(weight).then(() => { + clearTimeout(handle); + resolve(); + }); }); },