GithubHelp home page GithubHelp logo

co-series's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.