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

promiseutils

v0.1.9

Published

Set of function to handle an array or list of promises

Downloads

6

Readme

Synopsis

This project is aimed at providing utility functions in handling promises / array of promises.

Code Example

In the below example, we can see a way to chain multiple promises. The promise chain behaves as follows

  1. Trigger the first promise with the input arguments.
  2. Output of first promise is merged with initial set of arguments
  3. Output of Step2, is given as input to second promise
  4. Output of second promoise is merged with initial set of arguments + output of first promise
  5. ...

With the above method, we can effective solve simple workflows.


// To chain a bunch of promises see below example.
//
let getPromise1 = function(props) {
    return new Promise(function(resolve, reject) {
        axios.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + props.zipcode).then(function() {
            resolve({ zipcode: '33178' });

        }, function(resp) {
            reject(resp);
        });;
    });
}
let getPromise2 = function(props) {
    return new Promise(function(resolve, reject) {
        axios.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + props.zipcode).then(function() {
            resolve({ zipcode: '97658' });

        }, function(resp) {
            reject(resp);
        });;
    });
}
let getPromise3 = function(props) {
    return new Promise(function(resolve, reject) {
        axios.get("https://maps.googleapis.com/maps/api/geocode/json?address=" + props.zipcode).then(function() {
            resolve({ zipcode: '02345' });

        }, function(resp) {
            reject(resp);
        });;
    });
}


promiseutils.chain({ zipcode: '33025' }, [getPromise1, getPromise2, getPromise3]).then(function(resp) {
    console.log("RESOLVING");
}).catch(function(resp) {
    console.log(resp);
});

The standard behaviour of an array of promises with a race


// To race to first successfuly promise
//
//	- promises ( list of all promises )
//	- options ( List of options for future use )
//	returns the response of the first successful promise
//
promiseutils.race(promises,options).then(function(resp){
    //Handle success
}).catch(function(resp){
    //Handle failure
});

// To race to first failed promise
//
//	- promises ( list of all promises )
//	- options ( List of options - for future use )
//	returns the response of the first failed promise
//
promiseutils.raceToFail(promises,options).then(function(resp){
    //Handle success
}).catch(function(resp){
    //Handle failure
});

To sequence a list of promises to be executed one after another.


// To sequence all the promises one after the another
//
//	- variables ( Array of variables , For each variable a promise will be created using promiseFn )
//	- promiseFn ( This function will involved to get an instance of promise )
//	- options ( List of options, interval : seconds to wait before retry )
//	returns an array of all responses
//
promiseutils.seq(variables,promiseFn,options).then(function(resp){
    //Handle success
}).catch(function(resp){
    //Handle failure
});

To retry a promise a given number of times.

// To retry a promise a number of times
//
//	- arguments ( The inputs required to create a promise )
//	- promiseFn ( This function will involved to get an instance of promise )
//	- options ( List of options, maxretry : number of retries, interval : seconds to wait before retry )
//	returns the response of the successfull try
//
promiseutils.retry(arguments,promiseFn,options).then(function(resp){
    //Handle success
}).catch(function(resp){
    //Handle failure
});

Installation

To include this source in your nodejs application

Step1 :

npm install promiseutils

Step2 :

promiseutils = require('promiseutils');

License

MIT License.