mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 23:58:06 +01:00
3732c5f71e
ESLint rules have been added to enforce our JSDoc conventions. These rules were introduced by updating `@metamask/eslint-config` to v9. Some of the rules have been disabled because the effort to fix all lint errors was too high. It might be easiest to enable these rules one directory at a time, or one rule at a time. Most of the changes in this PR were a result of running `yarn lint:fix`. There were a handful of manual changes that seemed obvious and simple to make. Anything beyond that and the rule was left disabled.
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
/**
|
|
* Re-runs the given function until it returns a resolved promise or the number
|
|
* of retries is exceeded, whichever comes first (with an optional delay in
|
|
* between retries).
|
|
*
|
|
* @param {Object} args - A set of arguments and options.
|
|
* @param {number} args.retries - The maximum number of times to re-run the
|
|
* function on failure.
|
|
* @param {number} args.delay - The amount of time (in milliseconds) to wait in
|
|
* between retries. (Default: 0)
|
|
* @param {string} args.rejectionMessage - The message for the rejected promise
|
|
* this function will return in the event of failure. (Default: "Retry limit
|
|
* reached")
|
|
* @param {Function} functionToRetry - The function that is run and tested for
|
|
* failure.
|
|
* @returns {Promise<null | Error>} a promise that either resolves to null if
|
|
* the function is successful or is rejected with rejectionMessage otherwise.
|
|
*/
|
|
async function retry(
|
|
{ retries, delay = 0, rejectionMessage = 'Retry limit reached' },
|
|
functionToRetry,
|
|
) {
|
|
let attempts = 0;
|
|
while (attempts <= retries) {
|
|
if (attempts > 0 && delay > 0) {
|
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
}
|
|
|
|
try {
|
|
await functionToRetry();
|
|
return;
|
|
} catch (error) {
|
|
console.error(error);
|
|
} finally {
|
|
attempts += 1;
|
|
}
|
|
}
|
|
|
|
throw new Error(rejectionMessage);
|
|
}
|
|
|
|
module.exports = { retry };
|