retrial
Easily retry an async function without mangling it.
Last updated 4 years ago by dguttman .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install retrial 
SYNC missed versions from official npm registry.

retrial

Easily retry an async function without mangling it.

Example

var retrial = require('retrial')

retrial(5, finickyFunction, 1, 2, 3, function (err, r1, r2, r3) {
  if (err) return console.error(err) // 'unlucky' if we're unlucky ;)

  console.log(r1, r2, r3) // 2, 6, 12
})

function finickyFunction (arg1, arg2, arg3, cb) {
  if (Math.random() < 0.5) return cb(null, arg1 * 2, arg2 * 3, arg3 * 4)

  return cb(new Error('unlucky'))
}

API

retrial(count, fn[, args1...argsN], cb)

  • count: number of times to retry fn
  • fn: the async function you'd like to retry
  • args: the arguments to your function
  • cb: the callback to your async function

Why

I needed a simple retry helper, but I didn't want to mangle how I was calling my functions. Here's retrial vs async.retry:


retrial(2, request, 'http://guttman.io', function (err, res, body) {
  console.log(err, res, body)
})

// vs.

asyncRetry(2, function (cb) {
  request('http://guttman.io', function (err, res, body) {
    cb(err, [res, body])
  }),
  function (err, result) {
    var res = res[0]
    var body = res[1]
    console.log(err, res, body)
  }
})

license

MIT

Current Tags

  • 1.0.1                                ...           latest (4 years ago)

2 Versions

  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |