Skip to content

Commit

Permalink
makeBackoffMachine [nfc]: Use sleep() helper rather than re-implement.
Browse files Browse the repository at this point in the history
Move `sleep` and `delay` to top of async.js and use `sleep` in
makeBackoffMachine, to be concise, instead of re-implementing the
same thing.
  • Loading branch information
Chris Bobbe committed Jan 27, 2020
1 parent 47b4c01 commit ed75f0f
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/utils/async.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/* @flow strict-local */
import { isClientError } from '../api/apiErrors';

/** Like setTimeout(..., 0), but returns a Promise of the result. */
export function delay<T>(callback: () => T): Promise<T> {
return new Promise(resolve => resolve()).then(callback);
}

export const sleep = (ms: number = 0): Promise<void> =>
new Promise(resolve => setTimeout(resolve, ms));

/**
* Makes a machine that can sleep for a timeout that, until a ceiling is reached,
* grows exponentially in duration with the number of sleeps completed, with a
Expand Down Expand Up @@ -47,21 +55,13 @@ export const makeBackoffMachine = () => {
firstDuration * base ** numSleepsCompleted,
);

await new Promise(resolve => setTimeout(resolve, duration));
await sleep(duration);

numSleepsCompleted++;
},
};
};

/** Like setTimeout(..., 0), but returns a Promise of the result. */
export function delay<T>(callback: () => T): Promise<T> {
return new Promise(resolve => resolve()).then(callback);
}

export const sleep = (ms: number = 0): Promise<void> =>
new Promise(resolve => setTimeout(resolve, ms));

/**
* Calls an async function and if unsuccessful retries the call.
*
Expand Down

0 comments on commit ed75f0f

Please sign in to comment.