From 8d6a94f16888affdc043fd9e9a8702c4972389cb Mon Sep 17 00:00:00 2001 From: David Farr Date: Wed, 8 May 2024 14:12:01 -0700 Subject: [PATCH] Clear timeout in sync If this timeout is not cleared, a program will not halt until the timeout elapses even in the case where the durable promise completes. --- lib/core/promises/promises.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/core/promises/promises.ts b/lib/core/promises/promises.ts index 7b77fc4..57dac45 100644 --- a/lib/core/promises/promises.ts +++ b/lib/core/promises/promises.ts @@ -410,19 +410,23 @@ export class DurablePromise { await this.poll(); // set timeout promise + let timeoutId: NodeJS.Timeout | undefined; const timeoutPromise = timeout === Infinity ? new Promise(() => {}) // wait forever - : new Promise((resolve) => setTimeout(resolve, timeout)); + : new Promise((resolve) => (timeoutId = setTimeout(resolve, timeout))); // await either: // - completion of the promise // - timeout - await Promise.any([this.completed, timeoutPromise]); + await Promise.race([this.completed, timeoutPromise]); - // stop polling interval + // clear polling interval clearInterval(this.interval); + // clear timeout + clearTimeout(timeoutId); + // throw error if timeout occcured if (this.pending) { throw new Error("Timeout occured while waiting for promise to complete");