promised-retry
TypeScript icon, indicating that this package has built-in type declarations

0.6.0 • Public • Published

Promised Retry

npm version npm downloads Module type: CJS Types in JS js-semistandard-style Follow @voxpelli@mastodon.social

A generic promised based retry mechanism. Useful for eg. ensuring an available database or message queue connection

Installation

npm install promised-retry

Usage

Simple:

const Retry = require('promised-retry')
const { Client } = require('pg')

const retryInstance = new Retry({
  try: async () => {
    const db = new Client(require('../config').db);

    db.on('error', () => {
      retryInstance.reset();
      if (channels.length) {
        retryInstance.try();
      }
    });

    await db.connect();
  },
  success: db => {
    db.on('notification', self.processNotification.bind(self));

    channels.forEach(channel => {
      db.query('LISTEN ' + channel);
    });
  },
  end: db => { db.end(); }
});

Syntax

const retryInstance = new Retry(options);

Options

  • try - the function that will make up the attempt. Should return a Promise that's fulfilled or rejected depending on whether the attempt is a success or failure.
  • success - a function that's run on an successful attempt. Will be sent the result of the attempt and can return a modified result or a Promise.
  • end - a function that will be run on the closing of the retry script. Useful for when needing to fix a graceful shutdown of eg. a database. Can return a Promise if it needs to do something that the shutdown needs to wait for.
  • name – the name of the Retry attempt. Used in eg debugging.
  • setup – a function that will be run before the first attempt
  • name – the name of the Retry attempt. Used in eg debugging.
  • retryMin – minimum delay in milliseconds before a retry. Defaults to 0.
  • retryBase – the base of the delay exponent. Defaults to 1.2.
  • retryExponent – the maximum exponent of the delay exponent. If retries are higher than retryExponent, retryExponent will be used rather than the retry number. Defaults to 33 which means on average max delay of 3m 25s.
  • retryDelay – a function used to calculate the delay. Replaces the default exponent calculation. If it returns false the retries will be aborted.
  • retryLimit – maximum amount of retries. Defaults to unlimited retries.
  • log – a logger function. Defaults to console.log().

Methods

  • try – returns a Promise that will be resolved with the successful attempt or rejected if the retries were aborted due to the result of options.retryDelay() or because the retry instance was ended.
  • end – ends the Retry mechanism completely. Useful for ensuring a graceful shutdown. Returns a Promise that will be resolved when done.
  • reset – resets the Retry mechanism. Used in response to an event that eg. indicated that the connection the Retry mechanism managed to established has errored out. Will not result in a retry automatically. That has to be done manually if one wants one right away.

Lint / Test

npm test or to watch, install grunt-cli then do grunt watch

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.6.0734latest

Version History

VersionDownloads (Last 7 Days)Published
0.6.0734
0.5.05,246
0.4.05,738
0.3.03,009
0.2.31,619
0.2.24
0.2.14
0.2.04
0.1.04

Package Sidebar

Install

npm i promised-retry

Weekly Downloads

8,194

Version

0.6.0

License

MIT

Unpacked Size

17.1 kB

Total Files

6

Last publish

Collaborators

  • voxpelli