0.2.5 • Published 8 years ago

u-promised v0.2.5

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

u-promised

Promise based retry and backoff, bring your own Promises

Code Climate Test Coverage Issue Count Circle CI

npm install u-promised

API

retry(retries, fn)

  • retries:
    • number (0 - ...)
    • -1 (retry forever)
    • function (evaluated after each attempt; should return boolean)
  • fn: function that returns a promise
import { get } from 'highwire'
import { retry } from 'u-promised'

const fetchGoogle = () => get('http://google.com')

retry(3, fetchGoogle)
  .then((google) => console.log(google))
  .catch((err) => console.log('Oops, 4 attempts total', err))

// retry forever - no catch
retry(-1, fetchGoogle)
  .then((google) => console.log(google))

// evalute retries
const evalRetry = () => {
  let index = 0
  return () => {
    if (index === 5) return false
    index++
    return true
  }
}

retry(evalRetry(), fetchGoogle)
  .then((google) => console.log(google))
  .catch((err) => console.log('Oops, 6 attempts total', err))

backoff(delay, incrementor, retries, fn)

  • delay: initial delay after first failure
  • incrementor: increment to add to delay after each attempt
  • retries:
    • number (0 - ...)
    • -1 (retry forever)
    • function (evaluated after each attempt; should return boolean)
  • fn: function that returns a promise
import { get } from 'highwire'
import { backoff } from 'u-promised'

const fetchGoogle = () => get('http://google.com')
const SECOND = 1000

backoff(SECOND, SECOND, 3, fetchGoogle)
  .then((google) => console.log(google))
  .catch((err) => console.log('Oops, 4 attempts total', err))

// backoff forever
backoff(SECOND, SECOND, -1, fetchGoogle)
  .then((google) => console.log(google))

// evaluate backoff
const evalBackoff = () => {
  let index = 0
  return () => {
    if (index === 5) return false
    index++
    return true
  }
}

backoff(SECOND, SECOND, evalBackoff(), fetchGoogle)
  .then((google) => console.log(google))
  .catch((err) => console.log('Oops, 6 attempts total', err))

Developing

There's really not much to this codebase. Just run npm test. If you would like to contribute, please open an issue explaining what you would like to change/fix/add and make a pull request.