npm.io
3.0.1 • Published 4 years ago

failmenot

Licence
MIT
Version
3.0.1
Deps
0
Size
6 kB
Vulns
0
Weekly
0

failmenot

Run a failing function many times until it eventually passes or reaches execution limits.

Installation

npm install --save failmenot

Options

{
  maximumAttempts: null,
  maximumTime: null,
  retryDelay: 0
}

Example

Basic Usage
const failmenot = require('failmenot');

let attemptNumber = 1;
const successAttemptNumber = await failmenot(
  {
    maximumAttempts: 3
  },

  function () {
    if (attemptNumber < 3) {
      attemptNumber = attemptNumber + 1;
      throw new Error('should fail this attempt');
    }

    return 'worked on ' + attemptNumber;
  }
);

// successAttemptNumber === 'worked on 3';
Curried
failmenot -> options -> function -> result
const failmenot = require('failmenot/curried');

let attemptNumber = 1;

const applyRetry = failmenot({
  maximumAttempts: 3
});

const runner = applyRetry(
  function (caption) {
    if (attemptNumber < 3) {
      attemptNumber = attemptNumber + 1;
      throw new Error('should fail this attempt');
    }

    return caption + ' ' + attemptNumber;
  }
);

const successAttemptNumber = await runner('worked on')

// successAttemptNumber === 'worked on 3';