Skip to content

Commit

Permalink
Merge pull request #116 from resonatehq/fix/clear-sync-timeout
Browse files Browse the repository at this point in the history
Clear timeout in sync
  • Loading branch information
dfarr committed May 13, 2024
2 parents bf05d61 + 8d6a94f commit 19549fd
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/core/promises/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,23 @@ export class DurablePromise<T> {
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");
Expand Down

0 comments on commit 19549fd

Please sign in to comment.