PR suggestion: withRetry wrapper
LiranBri opened this issue · 1 comments
LiranBri commented
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
Deleted user commented
isn't that already mostly done with wrap
function, although different signature?