Skip to content

Commit

Permalink
Switch ordering of arguments to sync and wait
Browse files Browse the repository at this point in the history
  • Loading branch information
dfarr committed May 8, 2024
1 parent be4662e commit 481bb21
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export class Context {
}

// tight loop in case the promise is not yet resolved
await promise.wait(1000);
await promise.wait(Infinity, this.invocation.opts.poll);
}

/**
Expand Down
14 changes: 10 additions & 4 deletions lib/core/promises/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,20 @@ export class DurablePromise<T> {
* @param timeout - The time at which to stop polling if the promise is still pending.
* @returns A Promise that resolves when the Durable Promise is complete.
*/
async sync(frequency: number, timeout?: number): Promise<void> {
async sync(timeout: number = Infinity, frequency: number = 5000): Promise<void> {
if (!this.pending) return;

// reset polling interval
clearInterval(this.interval);
this.interval = setInterval(() => this.poll(), frequency);

// poll immediately for now
// we can revisit when we implement a cache subsystem
await this.poll();

// set timeout promise
const timeoutPromise =
timeout === undefined
timeout === Infinity
? new Promise(() => {}) // wait forever
: new Promise((resolve) => setTimeout(resolve, timeout));

Expand All @@ -429,8 +435,8 @@ export class DurablePromise<T> {
* @param timeout - The time at which to stop polling if the promise is still pending.
* @returns The promise value, or throws an error.
*/
async wait(frequency: number, timeout?: number): Promise<T> {
await this.sync(frequency, timeout);
async wait(timeout: number = Infinity, frequency: number = 5000): Promise<T> {
await this.sync(timeout, frequency);

if (this.resolved) {
return this.value();
Expand Down

0 comments on commit 481bb21

Please sign in to comment.