Skip to content

PR suggestion: withRetry wrapper #81

@LiranBri

Description

@LiranBri

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions