Compose chainable async methods
Compose chainable async methods.
npm install continue --save
var Continue = require'continue';var chain = Continue;var async = require'async'; // <~~ simply for usage example// Add methods to our chain sequencechainadd'map'asyncmapitems iterator next;;chainadd'sortBy'asyncsortByitems iterator next;;chainadd'filter'var self = this;var error;asyncfilteritemsiteratoritemif errerror = err;return;callbackmatched;;nexterror filteredItems;;;// Use our chainvar list =name: 'John'age: 25name: 'Jane'age: 20;chainlistsortBynextnull itemage;mapnextnull itemage;then// items now equals: [20, 25]// oops, an error may have occured;
The instance value returned from calling
Continue() is both a function and an object. The object is the instance and the function returns the chain sequence.
chain() supports the follow as initial values to start the chain:
- 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 chain.
var Continue = require'continue';var chain = Continue;var someValue = 'my value';chainsomeValue;
var Continue = require'continue';var chain = Continue;var someValue = 12345;chainsomeValue;
var Continue = require'continue';var chain = Continue;chainvar someValue = 'my value';nextnull someValue;;
These methods are available on the
Composes a chainable method on the chain. The method is added for all instances of that particular chain.
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 chain
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 Continue = require'continue';var chain = Continue;// Add methods to our chain sequencechainadd'map'iteratoritemsnexterr processItems;;;
These methods are available on the value returned from called
The methods available vary according to which methods have been added to the chain. In the usage example above, we've added
filter. These methods would be available on the chain as well as:
Each chain 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 Continue = require'continue';var chain = Continue;var someValue = 'my value';chainsomeValuesomeChainableMethodnextnull value + 's';then// processedValue == my values;
Runs each method in the chain b draining the chain queue on next tick.
- more detailed documentation
- allow callbacks on each chained method
- allow promises to be passed in as inititial value to chain
- Continue module examples
npm installnpm test