1.0.3 • Published 4 years ago

@jakezhang94/retrial v1.0.3

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

@jakezhang94/retrial

A pattern for retrial

/**
 * a Retrial pattern
 * @class Retrial
 * @property {function} handler - an async function of one single trail
 * @property {number} max - maximum number of retrial
 * @property {function|null} errorGenerator - a function that generates error when max limit reached
 *  if errorGenerator is not provided, run will reject the error produced by the last trial
 */
const Retrial = require('@jakezhang94/retrial')

const f = async function () {
  if (Math.random() > 0.9) {
    return 'foo'
  } else {
    throw 'bar'
  }
}

const MAX_RETRIAL_LIMIT = 10

const retrial = new Retrial(f, MAX_RETRIAL_LIMIT, () => 'You are out of luck!')

retrial.run()
.then(res => console.log('You will see foo here: ', res))
.catch(err => console.log('You will see \"You are out of luck!\" : ', err))
1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago