Skip to content

Commit c126fb7

Browse files
author
Chris Bobbe
committed
makeBackoffMachine [nfc]: Use sleep() helper rather than re-implement.
Move `sleep` and `delay` to top of async.js and use `sleep` in makeBackoffMachine, to be concise, instead of re-implementing the same thing.
1 parent 126adf4 commit c126fb7

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/utils/async.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
/* @flow strict-local */
22
import { isClientError } from '../api/apiErrors';
33

4+
/** Like setTimeout(..., 0), but returns a Promise of the result. */
5+
export function delay<T>(callback: () => T): Promise<T> {
6+
return new Promise(resolve => resolve()).then(callback);
7+
}
8+
9+
export const sleep = (ms: number = 0): Promise<void> =>
10+
new Promise(resolve => setTimeout(resolve, ms));
11+
412
/**
513
* Makes a machine that can sleep for a timeout that, until a ceiling is reached,
614
* grows exponentially in duration with the number of sleeps completed, with a
@@ -47,21 +55,13 @@ export const makeBackoffMachine = () => {
4755
firstDuration * base ** numSleepsCompleted,
4856
);
4957

50-
await new Promise(resolve => setTimeout(resolve, duration));
58+
await sleep(duration);
5159

5260
numSleepsCompleted++;
5361
},
5462
};
5563
};
5664

57-
/** Like setTimeout(..., 0), but returns a Promise of the result. */
58-
export function delay<T>(callback: () => T): Promise<T> {
59-
return new Promise(resolve => resolve()).then(callback);
60-
}
61-
62-
export const sleep = (ms: number = 0): Promise<void> =>
63-
new Promise(resolve => setTimeout(resolve, ms));
64-
6565
/**
6666
* Calls an async function and if unsuccessful retries the call.
6767
*

0 commit comments

Comments
 (0)