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

async-rerun

v1.1.2

Published

Perfect light-weight & asynchronious package for asynchroniously retry/rerun of any logic...

Downloads

52

Readme

async-rerun

Perfect light-weight & asynchronious package for asynchroniously retry/rerun of any logic...

Features

  • Calls any logic in with fixed max number of retrying/rerun buffer.
  • Asynchroniously call the logic with capacity to acll unlimited time of retry/rerun untill the positive response.
  • Can be adjusted with at what particular delay in (MS) we want to retry.
  • Logs the details of retries causes.

Usage

const { runAsync } = require('async-rerun');

function logicToRerun() {
  return new Promise((resolve, reject) => {
    // your code like api calling or anything but it should retrun the promise
    if(/* true response */){
      resolve(/* Data to return in true case.*/);
    } else {
      reject(/* Data to return in false/error/faulty case.*/);
    }
  })
}

/**
 * @param {Function} fn The function which will be used as parameter should return a PROMISE & SHOULD NOT HAVE ANY PARAMETER it's compulsory, as its a async retry mechanism.
 * @param {Number} maxRetries Max Retries in number.
 * @param {Number} delayInMS Delay time for retries in number as mili-seconds(ms).
 * @param {Boolean} alwaysRetry To retry always untill the positive response, If its true then maxRetries will not count.
 * @param {Boolean} showLogs To show logs for all retries.
 */

runAsync(fn, maxRetries, delayMs, alwaysRetry, showLogs).then((result) => {
   // This will return the exact same data that we resolved in promise of upper function.
}).catch(err => {
  // This will retrun the error object after the max retries finishes
})

Using in TS Env.

import { runAsync } from 'async-rerun';

// Now the same way implementations as above.....
//
//
//...

API

const { runAsync } = require('async-rerun');
//Or
const rerun = require('async-rerun');
//Or
import { runAsync } from 'async-rerun';
//Or
import * as rerun from 'async-rerun';

Example

function mongoDBConnect() {// any DB connection
  return new Promise((resolve, reject) => {
    // your code to connection to DB
    if(/* true response */){
      resolve(/* Data to return in true case.*/);
    } else {
      reject(/* Data to return in false/error/faulty case.*/);
    }
  })
}

/**
 * @param {Function} fn The function which will be used as parameter should return a PROMISE & SHOULD NOT HAVE ANY PARAMETER it's compulsory, as its a async retry mechanism.
 * @param {Number} maxRetries Max Retries in number.
 * @param {Number} delayInMS Delay time for retries in number as mili-seconds(ms).
 * @param {Boolean} alwaysRetry To retry always untill the positive response, If its true then maxRetries will not count.
 * @param {Boolean} showLogs To show logs for all retries.
 */

// Now the below async try to connect with DB and if its gets failed then it will retry to reconnect to DB with the retry time-span of 10 seconds delay for 10 times.
runAsync(mongoDBConnect, 10, 10000, false, false).then((result) => {
   // This will return the exact same data that we resolved in promise of upper function.
   // your logic to handle positive response.
}).catch(err => {
  // This will retrun the error object after the max retries finishes
   // your logic to handle negative response after 10 failed retries.
})

Example

// In this example we will use the async-rerun to retry unlimited of time untill it gets the positive response.
function connectToWebsocket() {// any DB connection
  return new Promise((resolve, reject) => {
    // your code for connection of websocket.

    /*if socket connection opens*/
    resolve(/*Data to return*/);
    
    //Or
    
    /*if socket connection gets any error*/
    reject(/*Data to return in failed state of promise*/);
   
  })
}

/**
 * @param {Function} fn The function which will be used as parameter should return a PROMISE & SHOULD NOT HAVE ANY PARAMETER it's compulsory, as its a async retry mechanism.
 * @param {Number} maxRetries Max Retries in number.
 * @param {Number} delayInMS Delay time for retries in number as mili-seconds(ms).
 * @param {Boolean} alwaysRetry To retry always untill the positive response, If its true then maxRetries will not count.
 * @param {Boolean} showLogs To show logs for all retries.
 */

// Now the below async try to connect with websocket and if its gets failed then it will retry to reconnect to websocket with the retry time-span of 10 seconds delay for unlimited retries with logs.
runAsync(connectToWebsocket, 10, 10000, true, true).then((result) => {
   // This will return the exact same data that we resolved in promise of upper function.
   // your logic to handle positive response.
}).catch(err => { // in this state catch will not get called as we are retrying without any retry limit
   // your logic to handle error in current promise.
})