1.0.0 • Published 7 years ago

@canastro/retry v1.0.0

Weekly downloads
1
License
ISC
Repository
-
Last release
7 years ago

build status npm version codecov XO code style

Retry

Naive implementation of a retry system.

Features

  • Max attempts
  • Add delay between retries
  • Callback to interrupt retries

Installation

npm install --save @canastro/retry

Usage

const retry = require('@canastro/retry');
const callback = (min, max) => () => (Math.random() * (max - min)) + min;

const isResolved = result => result > 19;

const options = {
    maxAttempts: 5,
    intervalTimeout: 1000
};

/**
 * This example will call a function that generates
 * a number between 2 and 20, and it will try for 5 times
 * to get a result higher then 19, having a delay
 * of 1000ms between each execution
 */
retry(callback(2, 20), options, isResolved)
    .then(result => {
        console.log('result: ', result);
    })
    .catch(() => {
        console.log('error');
    });

PS: Check sandbox for more examples, you can try them by running node sandbox/#filename#

Parameters

ParameterTypeDefaultDescription
callbackFunctionUser's function to be retried
optionsObject{}User's options
options.maxAttemptsNumber10Number of max attempts
options.intervalTimeoutNumber0msNumber of milliseconds to wait before retrying
isResolvedFunction() => trueFunction to be used as a stop condition

Returns

  • If a isResolved function is provided it will return a fulfilled promise when this returns true;
  • If no isResolved was given it will return true once the provided callback is correctly fulfilled;
  • It will return a rejected promise the max attempts was reached