-
Notifications
You must be signed in to change notification settings - Fork 85
Open
Description
Hi, I wrote an higher-level function util withRetry which makes the usage of the library very easy and also supports promises.
this is what I wrote:
const retry = require('retry');
function withRetry(callback) {
return (...args) => {
const operation = retry.operation({ retries: 5 });
return new Promise((resolve, reject) => {
operation.attempt(async function (currentAttempt) {
try {
const result = await callback(...args);
resolve(result);
} catch (err) {
console.log(`attempt #${currentAttempt} failed. error: ${err.message}`);
if (operation.retry(err)) {
return;
}
reject(err);
}
});
});
};
}
module.exports = withRetry;now any function could be enhanced with retry logic using a single line. e.g:
async functuin myFunc(a, b) {
// .. bla bla async logic ..
return a + b
}
module.exports = withRetry(myFunc);
if it feels useful, I can make the code more generic, and contribute as PR
faboulaws
Metadata
Metadata
Assignees
Labels
No labels