Compose a series of chainable async methods
Compose a series of chainable async methods. Supports callbacks AND promises.
Supports Node and Browserify
npm install series --save
var Series = require'series';var series = Series;var async = require'async'; // <~~ simply for usage example// Add methods to our series sequenceseriesadd'map'asyncmapitems iterator next;;seriesadd'sortBy'asyncsortByitems iterator next;;seriesadd'filter'var self = this;var error;asyncfilteritemsiteratoritemif errerror = err;return;callbackmatched;;nexterror filteredItems;;;// Use our seriesvar list =name: 'John'age: 25name: 'Jane'age: 20;serieslistsortBynextnull itemage;mapnextnull itemage;// Support for callbacksthen// items now equals: [20, 25]// oops, an error may have occured;
The instance value returned from calling
Series() is both a function and an object. The object is the instance and the function returns the series chain.
series() supports the following as initial values to start the series:
- Primitive - strings, numbers, etc.
- Array - an array of primitives or objects
- Function - a function that is passed a
nextcallback that must be called with the following:
next(err, value). The value passed as the second argument becomes the initial value of the series.
- Promise - a Promises/A+ compliant value
var Series = require'series';var series = Series;var someValue = 'my value';seriessomeValue;
var Series = require'series';var series = Series;var someValue = 12345;seriessomeValue;
var Series = require'series';var series = Series;seriesvar someValue = 'my value';nextnull someValue;;
var Series = require'series';var Promise = require'promise'; // <~~ npm install promisevar series = Series;var promise =// Some logic;seriespromise;
These methods are available on the
Composes a chainable method on the series. The method is added for all instances of that particular series.
A string that will be used as the method name
A callback function that is passed the following parameters
items- any value that you would like passed to the first method in the series
iterator- the method called on the value from the previous method that performs some sort of user defined operation
next- the callback once all items in the list or collection have been processed.
var Series = require'series';var series = Series;// Add methods to our series sequenceseriesadd'map'iteratoritemsnexterr processItems;;;
These methods are available on the value returned from calling
The methods available vary according to which methods have been added to the series. In the usage example above, we've added
filter. These methods would be available on the series as well as:
Each series sequence returns a promise. Refer to the Promises/A+ spec for more details.
This is called once all chained methods have processed the initial value, unless an error has occurred.
This is called if an error occurs anywhere when processing the initial value in any of the chainable methods. If an error occurs, all processing stops.
var Series = require'series';var series = Series;var someValue = 'my value';seriessomeValuesomeChainableMethodnextnull value + 's';then// processedValue == my values;
Each method in the series can take a callback if you'd rather not use promises. Each callback will be called at the completion of each method.
var Series = require'series';var series = Series;seriessomeValuesomeChainableMethodnextnull value + 's';// arg1 == value + 's'someOtherMethodnextnull;// err == null
npm installnpm test