npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promised-retry

v0.6.0

Published

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

Downloads

34,029

Readme

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