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

attempt

v1.0.1

Published

Automatically retry functions that fail, in crazily customizable ways.

Downloads

815

Readme

Attempt Build Status Code Climate

Retries functions that throw or call back with an error, in crazily customizable ways.

Installation

In your project folder, type:

npm install attempt

Usage

Whenever you have code that looks like this:

function flakyApiCall(function(err, result) {
    if (err)
        console.log('Flaky API died AGAIN!', err);
    else
        doSomething(result);
});

Just drop that code in an attempt!

var attempt = require('attempt');
attempt(
	function() {
		flakyApiCall(this);
	},
	function(err, result) {
        if (err)
            console.log('Flaky API failed 3 times.', err);
        else
            doSomething(result);
    });

Details

Attempt will re-run your attempted function if it throws an error or if it calls back with a non-falsey first argument (following the first-arg-is-an-error standard Node.js convention). The function call looks like this:

attempt([options], tryFunc, [callback])

tryFunc is called with one argument: attempts. It is the number of times the tryFunc has been run before.

attempt(function(attempts) {
	if (attempts)
		console.log('This is retry #' + attempts);
	else
		console.log('This is the first attempt!');
});

Options

The following options are set per request:

retries

Default: 2. The number of times to retry the tryFunc before giving up and sending the error to the callback.

attempt(
	{ retries: 15 },
	function() {
		flakyApiCall(this);
	},
	function(err, result) {
		if (err)
			console.log('Failed first attempt + 15 retries = 16 failures.', err);
		else
			doSomething(result);
	});

interval

Default: 0. The number of milliseconds to wait between attempts.

attempt(
	{ interval: 5000 },
	function() {
        flakyApiCall(this);
    },
    function(err, result) {
        if (err)
            console.log('3 attempts * 5 seconds = 15 seconds of failure.', err);
        else
            doSomething(result);
    });

factor

Default: 1. The factor by which the interval should be multiplied per attempt. If set to 2 with an interval of 5000, the first retry will execute after 5 seconds, the second after 10, the third after 20, and so on.

This allows an exponential back-off scheme. For a smaller gap between retries, floats like 1.2 can be used to grow the interval at a slower rate.

onError

Default: null. Function to call when the tryFunc fails with an error. The first argument is the error.

attempt(
	{
		onError: function(err) {
			console.log(err);
		}
	},
	function() { flakyApiCall(this); },
	function(err, result) { /* ... */ });

The second argument, if it exists, is a callback function that will need to be called in order for the next attempt to continue. This is useful if you have to do something asynchronous to fix the error.

attempt(
	{
		onError: function(err, done) {
    		log.write(err, done);
    	}
    },
    function() { flakyApiCall(this); },
	function(err, result) { /* ... */ });

By default, calling done() will ignore the retry interval. If you still want it to be observed, call done(true).

max

Default: Infinity. The maximum number of milliseconds to wait before retrying. If the interval or factor causes a wait time larger than 'max', 'max' will be used.

random

Default: 0. Increase the wait interval by a random factor. Generally, this should be a number between 0 (no randomness) and 1 (wait time could double). For example, if .5 is used, the interval could be anywhere between 100% and 150% of its original calculated time.

attempts

Default: 0. The number of attempts to fake Attempt into believing were already completed. This is mostly used by Attempt internally, but can be useful for hacking interval times.

License

Attempt is distributed under the MIT license.

Credits

Attempt was created by Tom Frost in 2012. Because webservice APIs be flaky, man.