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-series

v3.0.2

Published

Run in series with co

Downloads

40

Readme

co-series.js

Run in series with co

Current status

NPM version Build Status Dependency Status Dev dependency Status Coverage Status

v3.x.x uses native JS Promises rather than Bluebird by default. If you prefer Bluebird, use co-series-bluebird.

Usage

This small module shims an asynchronous function to ensure it queues each execution after the the previous execution is complete.

Really comes into it's own when used in conjunction with co and generators. Then you can use native flow control methods (or something like lodash) to control execution.

Generators

var co = require('co');

var order;
var fn = function*(num) {
    order.push('start ' + num);
    yield Promise.resolve();
    order.push('end ' + num);

    return num * 10;
}

// execute function in parallel
co(function*() {
    order = [];
    var result = yield [1, 2, 3].map(co.wrap(fn));
    order.push('finished ' + result);

    // order = ['start 1', 'start 2', 'start 3', 'end 1', 'end2', 'end 3', 'finished [10, 20, 30]']
});
var series = require('co-series');

// execute function in series
co(function*() {
    order = [];
    var result = yield [1, 2, 3].map(series(fn));
    order.push('finished ' + result);

    // order = ['start 1', 'end 1', 'start 2', 'end2', 'start 3', 'end 3', 'finished [10, 20, 30]']
});

Promises

You don't need to use co and generators!

var order;
var fn = function(num) {
    order.push('start ' + num);
    return Promise.resolve().then(function() {
        order.push('end ' + num);
        return num * 10;
    });
}

// execute function in parallel
order = [];
Promise.all([1, 2, 3].map(fn));

// order = ['start 1', 'start 2', 'start 3', 'end 1', 'end2', 'end 3']
var series = require('co-series');

// execute function in series
Promise.all([1, 2, 3].map(series(fn)));

// order = ['start 1', 'end 1', 'start 2', 'end2', 'start 3', 'end 3']

Errors/rejections

If an error is thrown or the promise returned by fn is rejected, further iterations are cancelled.

Execution order

On the first iteration, fn is called immediately. Thereafter, each iteration awaits the previous iteration to complete.

var promise = Promise.all([1, 2, 3].map(series(fn)));
order.push('sync');

// order = ['start 1', 'sync', 'end 1', 'start 2', 'end2', 'start 3', 'end 3']

If you wish the first iteration to execute on the next tick, pass option immediate as false

var promise = Promise.all([1, 2, 3].map(series(fn, {immediate: false})));
order.push('sync');

// order = ['sync', 'start 1', 'end 1', 'start 2', 'end2', 'start 3', 'end 3']

series.use(Promise)

Creates a new instance of co-series, which uses the Promise implementation provided (by default co-series uses native JS promises).

var Bluebird = require('bluebird');
var series = require('co-series').use(Bluebird);

// now use `co-series` in the usual way
var fn = series(function() {});

var promise = fn();

console.log(promise instanceof Bluebird); // true

If .use() is called without an argument, a new instance of co-series is created using native JS Promises.

Tests

Use npm test to run the tests (or npm run test-harmony on Node v0.12.x). Use npm run cover to check coverage.

Changelog

See changelog.md

Issues

If you discover a bug, please raise an issue on Github. https://github.com/overlookmotel/co-series/issues

Contribution

Pull requests are very welcome. Please:

  • ensure all tests pass before submitting PR
  • add an entry to changelog
  • add tests for new features
  • document new functionality/API additions in README