JSPM

iterable-backoff

0.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1542
  • Score
    100M100P100Q117316F
  • License ISC

Backoff generators usable as simple iterables

Package Exports

  • iterable-backoff

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (iterable-backoff) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

iterable-backoff Build Status

Backoff generators usable as simple iterables

Install

Installation of the npm package:

> yarn add iterable-backoff

# Or

> npm install --save iterable-backoff

Usage

import { factorial } from 'iterable-backoff'

async function fetch (url) {
  for (const delay of factorial().toMs().take(5)) {
    try {
      return await got(url) // or any promise-returning HTTP lib
    } catch (error) {
      console.warn(error)
      await Bluebird.delay(delay) // or any promise-returning timer
    }
  }

  throw new Error('too many tries')
}

Generators

power(power = 2)

Exponential sequence

fibonacci()

Fibonacci sequence

exponential(base = 2)

Exponential sequence

Methods

addNoise(factor = 0.1)

Add a noise to the sequence, proportional to the value (default is 10%).

Particularly useful when the backoff is used to wait access for a shared resource and you don't want multiple consumer retrying at the same time.

for (const delay of power().addNoise()) {
  // ...
}

clamp(min, max)

Clamps the value within inclusive min and max bounds.

for (const delay of exponential().clamp(null, 10)) {
  // ...
}

map(fn)

Applies a custom function to each value of the sequence.

Clamps the value within inclusive min and max bounds.

for (const delay of fibonacci().map(x => x / 2)) {
  // ...
}

take(n)

Limits the sequence to at most n values.

You usually want to use this if you do not want to keep retrying for ever.

for (const delay of power().take(10)) {
  // ...
}

Development

# Install dependencies
> yarn

# Run the tests
> yarn test

# Continuously compile
> yarn dev

# Continuously run the tests
> yarn dev-test

# Build for production (automatically called by npm install)
> yarn build

# Commit changes
> yarn cz

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet