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

    1.1.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

    Keywords

    none

    Install

    npm i promise-backoff

    DownloadsWeekly Downloads

    36

    Version

    1.1.4

    License

    MIT

    Unpacked Size

    27.8 kB

    Total Files

    17

    Last publish

    Collaborators

    • tjmehta