promise-backoff
TypeScript icon, indicating that this package has built-in type declarations

2.0.4 • Public • Published

promiseBackoff

backoff an async task using iterables

Installation

npm i --save promise-backoff

Usage

Supports both ESM and CommonJS

// esm
import backoff from 'promise-backoff`
// commonjs
const backoff = require('promise-backoff')

Example

import backoff from 'promise-backoff'

const opts = {
  // required
  timeouts: [10, 20, 30] // any iterable (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols)
  // optional w/ defaults shown
  maxTimeout: Infinity,
  minTimeout: 0,
  jitter: function fullJitter(val) {
    return val * Math.random()
  },
  signal: new AbortController().signal
}
let i = 0
await backoff(opts, async (retry) => {
  try {
    const res = await fetch('https://codeshare.io')
    if (res.status >= 500) {
      // 50X status error, retry if there are attempts left
      return retry(new Error(`status: ${res.status}`))
    }
    return res
  } catch(err) {
    // network error, retry if there are attempts left
    return retry(err)
  }
})

License

MIT

/promise-backoff/

    Package Sidebar

    Install

    npm i promise-backoff

    Weekly Downloads

    21

    Version

    2.0.4

    License

    MIT

    Unpacked Size

    26.2 kB

    Total Files

    18

    Last publish

    Collaborators

    • tjmehta