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

co-utils

v0.3.1

Published

Calls an array of promises in series using co

Downloads

141

Readme

co-utils

Runs an array of yieldables in series using co

.series(arr, [ctx], [args], [timeout]) Calls an array of yielables in series

arr Array of yieldables
ctx This context (Optional)
args Arguments passed to each yieldable (Optional)
timeout Optional number of a timeout in milliseconds. Defaults to 30000

let co = require('co-series');

// co can be used as usual
co(function* () {

});

let promises = [
    Promise.resolve('a');
    Promise.resolve('b');
    Promise.resolve('c');
];

// calls an array of yieldables in series
co.series(promises).then(function(result) {
    // result === ['a', 'b', 'c']
});

// if one of the promises rejects or an error will be thrown,
// then the main promise will fail

let promises = [
    Promise.resolve('a');
    Promise.resolve('b');
    Promise.reject('Something went wrong');
];

co.series(promises).then(function(result) {
    // this will not be called
}).catch(function(err) {
    // err === 'Something went wrong'
});

.pipe(arr, [ctx], pipeObj, [timeout])

Pipes an object through an array of yieldables in series

arr Array of yieldables
ctx This context (Optional)
pipeObj Object to be piped through yieldables arr
timeout Optional number of a timeout in milliseconds. Defaults to 30000

let arr = [
    function * (obj) { obj.a = true; return obj },
    function * (obj) { obj.b = true; return obj },
    function * (obj) { obj.c = true; return obj }
];

obj = {};
co.pipe(arr, pipeObj).then(function(result) {
    // result looks like:
    // { a: true, b: true, c: true }
})

yieldables

We call yieldable a function, a generator or a promise, supported by co-series.

Promises

var promise = new Promise(function(resolve, reject) {
    resolve('apple');
});

Generators

var generator = function *() {
    return 'banana';
}

Callback functions

var callback = function(done) {
    done(null, 'coconut');
}

Callbacks with promises

var promiseCallback = function(promise) {
    promise.resolve('date');
}

Promise returning functions

var promiseReturn = function(promise) {
    return Promise.resolve('elderberry');
}

Call it all together

co.series([
    promise,
    generator,
    callback,
    promiseCallback,
    promiseReturn
]).then(function(result) {
    console.log(result);

    //Prints ['apple', 'banana', 'coconut', 'date', 'elderberry'] on the screen
}).catch(function(err) {
    // Error handling
});

Arguments and this context

Passing arguments or a this context to yieldable is very easy. co.series accepts a this context as second parameter and an arguments array as third parameter. Both of them are optional.

let ctx = {
    prefix: '#'
};

let args = ['apple', 'banana'];

let fn1 = function *(arg1, arg2) {
    return this.prefix + arg1;
}

let fn2 = function *(arg1, arg2) {
    return this.prefix + arg2;
}
co.series([fn1, fn2], ctx, args).then(function(result) {
    //result contains ['#apple', '#banana']
}).catch(function(err) {
    // Error handling
});

Timeout

co.series timeouts after 30 seconds by default. The timeout can be changed by passing a number of milliseconds as fourth or last parameter

let ctx = {
    prefix: '#'
};

let args = ['apple', 'banana'];

let fn1 = function (arg1, arg2, done) {
    // never call done()
}

let fn2 = function *(arg1, arg2) {
    return this.prefix + arg2;
}
co.series([fn1, fn2], ctx, args, 500).then(function(result) {
    // this part is not called
}).catch(function(err) {
    // This is thrown, err contains an 'Timeout' error
});