This repository was archived by the owner on Dec 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutils.js
More file actions
39 lines (37 loc) · 1.26 KB
/
utils.js
File metadata and controls
39 lines (37 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/**
* Resolves when the amount of time specified by delay has elapsed.
*/
export async function wait(delay) {
return new Promise((resolve) => {
window.setTimeout(resolve, delay);
});
}
/**
* Calls the given callback, retrying if it rejects or throws an error.
* The delay between attempts increases per-try.
*
* @param {function} callback Function to call
* @param {number} maxRetries Number of times to try calling
* @param {number} delayFactor Multiplier for delay increase per-try
* @param {number} initialDelay Initial delay in milliseconds
*/
export async function retry(callback, maxRetries = 5, delayFactor = 2, initialDelay = 1000) {
/* eslint-disable no-await-in-loop */
let delay = initialDelay;
let lastError = null;
for (let k = 0; k < maxRetries; k++) {
try {
// If we don't await here, the callback's promise will be returned
// immediately instead of potentially failing.
return await callback();
} catch (err) {
lastError = err;
await wait(delay);
delay *= delayFactor;
}
}
throw lastError;
}