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

promise-attempt

v2.0.0

Published

Attempt tries to resolve promises

Downloads

12

Readme

promise-attempt NPM version Build Status Coverage Status

Attempt tries to resolve promises.

  • Can try to get valuable data from server over unstable mobile networks.
  • Can handle some unexpected network lags with 0 code or timeouts.

For example, user of your service have some critical data and you want to deliver it to your server. User browsing under unstable and slow cellular network. You may give up on first fail or try to fix this problem, using callback hell or use this solution :)

Assume you have rule which defines behaviour of repeating requests:

  • each next repeat should be performed in N * 500ms, where N is attempt number
  • error.status >= 500 - no reason to repeat, your service is totally down
  • error.status === 400 - no reason to repeat, something wrong with input data
  • error.status === 0 (aka abort) - repeat
  • if number of repeats is more than 5 - ask user continue to repeat or not
  • if number of repeats is more than 10 - do not repeat

And instead of calling promise directly

$.getJSON('/json')
.then(resolve, reject, progress);

wrap it with

new Attempt(function () {
    return $.getJSON('/json');
},
function repeatRules(err, attemptNo) {
    // no reason to repeat
    if (err && (err.status === 400 || err.status >= 500)) {
        return false;
    }
    
    // if number of repeats is more than 10 - do not repeat
    if (attemptNo > 10) {
        return false;
    }
    
    // if number of repeats is more than 5 - ask user what repeat or not
    if (attemptNo > 5) {
        return askUserWhatToDo().pipe(function () {
             return attemptNo * 500;
        });
    }
    
    // each next repeat should be performed in N * 500ms
    return attemptNo * 500;
})
.then(resolve, reject, progress);

Live example

Examples

Configure attempt

By default Attempt tries to use global Promises, but you can specify your own promises.

attempt.configure(require('vow').Promise);

Or use old-style promises approach:

attempt.configure(function (handler) {
    var dfd = $.Deferred();
    try {
        handler(dfd.resolve, dfd.reject, dfd.progress);
    } catch (e) {
        dfd.reject(e);
    }
    return dfd.promise();
});

Synchronous example

attempt(function promiseFactory() {
   return $.getJSON('/json');
},
function decision(err, attemptNo) {
    // if not timeout (null)
    // and 0 < status < 500 - no reason to continue
    if (err && err.status > 0 && err.status < 500) {
        return false || null || void 0;
    }

    // To many attempts - network or server totally down
    if (attemptNo > 5) {
        return false;
    }

    // repeat request in N * 2000 ms
    return attemptNo * 2000;
})
.then(resolve, reject, progress);

function resolve(data) {
    return processData(data);
}

function reject(error) {
    console.log('I tried several times... and fail :(', error);
}

function progress(error, attemptNo) {
    console.log('Failed to load, retrying...', error, attemptNo);
}

Async example

attempt(function () {
   return $.getJSON('/json');
},
function (err, attemptNo, decide) {
   setTimeout(function () {
       decide(Math.random() > 0.5 ? true : false);
   }, 1000)
})
.then(resolve, reject, progress);

Promise example

attempt(function () {
   return $.getJSON('/json');
},
function (err, attemptNo) {
   // askUserWhatToDo - a Promise factory
   return askUserWhatToDo(err, attemptNo);
})
.then(resolve, reject, progress);

Attempt number in promise Factory function

attempt(function (err, attemptNo) {
    // pass attempt to server (for logging)
    return $.getJSON('/json?attempt=' + attemptNo);
},
function (err, attemptNo) {
   // askUserWhatToDo - a Promise factory
   return askUserWhatToDo(err, attemptNo);
})
.then(resolve, reject, progress);