co-series
Run in series with co
Last updated 2 years ago by overlookmotel .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install co-series 
SYNC missed versions from official npm registry.

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
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 0
Last Month 8
Dependencies (2)
Dev Dependencies (9)

Copyright 2014 - 2016 © taobao.org |