You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
658 B
26 lines
658 B
3 years ago
|
const { exitWithError } = require('./exit-with-error');
|
||
|
|
||
|
/**
|
||
|
* Run the given function, retrying it upon failure until reaching the
|
||
|
* specified number of retries.
|
||
|
*
|
||
|
* @param {number} retries - The number of retries upon failure to attempt.
|
||
|
* @param {function} functionToRetry - The function that will be retried upon failure.
|
||
|
*/
|
||
|
async function retry(retries, functionToRetry) {
|
||
|
let attempts = 0;
|
||
|
while (attempts <= retries) {
|
||
|
try {
|
||
|
await functionToRetry();
|
||
|
return;
|
||
|
} catch (error) {
|
||
|
console.error(error);
|
||
|
} finally {
|
||
|
attempts += 1;
|
||
|
}
|
||
|
}
|
||
|
exitWithError('Retry limit reached');
|
||
|
}
|
||
|
|
||
|
module.exports = { retry };
|