Backoff generators usable as simple iterables
Installation of the npm package:
> yarn add iterable-backoff
# Or
> npm install --save iterable-backoff
import { fibonacci } from "iterable-backoff";
async function fetch(url) {
for (const delay of fibonacci()
.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");
}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()) {
// ...
}Clamps the value within inclusive min and max bounds.
for (const delay of exponential().clamp(null, 10)) {
// ...
}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)) {
// ...
}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)) {
// ...
}Converts the sequence's values to milliseconds (from seconds).
for (const delay of exponential(3)
.take(10)
.toMs()) {
// ...
}# Install dependencies
> npm ci
# Run the tests
> npm test
# Continuously compile
> npm run dev
# Continuously run the tests
> npm run dev-test
# Build for production (automatically called by npm install)
> npm run build
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.
ISC © Julien Fontanet
