New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

stubbornizeasync

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stubbornizeasync

Helper for making an async function stubborn (retry the operation on failure)

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
884
8.33%
Maintainers
1
Weekly downloads
 
Created
Source

stubbornizeasync

Create a "stubborn" version of an asynchronous function so that the operation is retried a configurable number of times in case the original function returns an error.

var stubbornizeAsync = require('stubbornizeasync');

// This function will fail ~10% of the time
function myFlakyFunction(cb) {
    setTimeout(function () {
        if (Math.random() > .9) {
            cb(new Error("whoops"));
        } else {
            cb();
        }
    }, 10);
}

// This function will fail ~1% of the time:
var stubbornFunction = stubbornizeAsync(myFlakyFunction, {numRetries: 1});

// This function will fail ~0.01% of the time:
var evenMoreStubbornFunction = stubbornizeAsync(myFlakyFunction, {numRetries: 3});

evenMoreStubbornFunction(function (err) {
    if (err) {
        throw err;
    } else {
        // Yay!
    }
});

By default the stubbornized function retries the original function 3 times (so it's run a maximum of 4 times), and it waits 250 milliseconds between the attempts. These parameters can be adjusted by providing an options object as the second parameter to stubbornizeAsync:

var veryStubbornAndVeryPatientFunction = stubbornizeAsync(myFlakyFunction, {numRetries: 10, delayMsec: 10000});

Installation

Make sure you have node.js and npm installed, then run:

npm install stubbornizeasync

License

3-clause BSD license -- see the LICENSE file for details.

Keywords

stubborn

FAQs

Package last updated on 03 May 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts