co-series.js
Run in series with co
Current 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 = ; var order;var { order; Promise; order; return num * 10;} // execute function in parallel;
var series = ; // execute function in series;
Promises
You don't need to use co and generators!
var order;var { order; return Promise;} // execute function in parallelorder = ;Promiseall1 2 3; // order = ['start 1', 'start 2', 'start 3', 'end 1', 'end2', 'end 3']
var series = ; // execute function in seriesPromiseall1 2 3; // 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 = Promiseall1 2 3;order; // 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 = Promiseall1 2 3;order; // 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 = ;var series = ; // now use `co-series` in the usual wayvar fn = ; var promise = ; console; // 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