Skip to content

canastro/retry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 generateNumber = (min, max) => () => (Math.random() * (max - min)) + min;

const isBiggerThanNineteen = 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(generateNumber(2, 20), options, isBiggerThanNineteen)
    .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

Parameter Type Default Description
callback Function User's function to be retried
options Object {} User's options
[options.maxAttempts] Number 10 Number of max attempts
[options.intervalTimeout] Number 0ms Number of milliseconds to wait before retrying
isResolved Function () => true Function 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

Releases

No releases published

Packages

No packages published