Serial
Serial provide a simple way for management of serial async call, so you can remember the API just by a glance.
If you think async and promise is somehow complecated, consider to use chemzqm/parallel and chemzqm/serial to make your life easier.
Installation
Via npm:
npm install node-serial
Via component:
component install chemzqm/serial
Features
- Unified error handling.
- Timeout support for individual callback.
- Immediate finished when error occur.
- Get previous callback result through
ctx.res
. - Save and get properties on
ctx
object. - No magic on finished callback, just one function.
Test on node and browser
You must have component
installed for browser usage and mocha
, mocha-phantomjs
installed for testing.
$ npm install -g component mocha mocha-phantomjs$ git clone git@github.com:chemzqm/serial.git$ cd serial && npm install$ make#run test on server side$ make test#run test on phantomjs$ make phantomjs
Example
{;}var Serial = ; //The name is 'node-serial' in node environmentvar serial = ;serial;1 2 3serial;
API
Serial()
Init new Serial instance.
.timeout(Number)
Set the timeout to number ms
, default is 10s
.
Note this timeout if for individual callback function.
.add(Function)
Add Function to serial, the first argument is a callback function, it should be called with error
as first argument and result you need as secound argument. the secound argument is a ctx
obejct, it's res
attribute contains the result of the previous function call, you can save any object on ctx
as you need.
.done(Function)
The callback function is called with error
(null or undefined if not exist) and the result array when all the request get finished (or timeout reached).
Note, this function should only be called once.
License
MIT