Simple control-flow library for node.js that makes parallel execution, serial execution and error handling painless.
Is a simple control-flow library for node.js that makes serial execution, parallel execution and error handling painless. Inspired by step twostep was based on Tim Caswell (step author) gist, but later it was refactored and almost totally rewritten. Twostep follows and extends the ideas behind the step.
- serial and parallel (with results grouping support) execution
- simple error handling
- simplified error handling for common use case (when error handled at the last step)
- ability to pass the arbitrary values between steps (using
- pure js code (no dependencies, < 200 lines of code)
- full test coverage (100%)
- super battle-tested (we ubiquitously use twostep in a big project (50 000+ lines of code))
npm install twostep
All steps (functions) passed to the
Step will be executed in series. Inside
this.slot() for wating of async call,
passing value to the next step or
this.makeGroup() for creating group (for
having results grouped to array) can be called.
var Step = require'twostep'Stepfs = require'fs';Step// pass filename to the next step as second argument (error always first)thispass__filename + '.bak';// read the file content, it will be passed as third argument (because// this.pass was called before)fsreadFile__filename 'utf8' thisslot;// if we have an error, throw it to skip this stepif err throw err;thispassbakFile;fsreaddir__dirname thisslot;fswriteFilebakFile content thisslot// `readdir` and `writeFile` will be exeuted in parallel when both of// them done next step will be calledif err throw err;console.log'%s successfully written' bakFile;thispassdirContent;// make the groupvar group = thismakeGroup;// wait for ordered results using group.slot() - all results// will be passed as an array with same order (as at calls)dirContentforEachfsstatname groupslot;;if err throw err;var fileNames = dirContentfilterreturn statsiisFile;;console.log'files in dir: %s' fileNames;// don't call next step manually, it's the last step if no error// this step will be called only if an error was occured at any step// above, so here we can handle the error.console.log'Error occured: ' errstack || err;processexit1;;
In the example above we did
if (err) throw err; at the start of every step to
pass the error (if it exists) to the next step for handle the error at the last
step. We can avoid writing this annoying line using
example described above transforms to
var Steppy = require'twostep'Steppyfs = require'fs';Steppythispass__filename + '.bak';fsreadFile__filename 'utf8' thisslot;thispassbakFile;fsreaddir__dirname thisslot;fswriteFilebakFile content thisslotconsole.log'%s successfully written' bakFile;thispassdirContent;var group = thismakeGroup;dirContentforEachfsstatname groupslot;;var fileNames = dirContentfilterreturn statsiisFile;;console.log'files in dir: %s' fileNames;console.log'Error occured: ' errstack || err;processexit1;;
Steps container accepts functions and executes them in series. If error is
occured inside step it will be passed to the next step as first argument. First
argument of the step is always an error (falsy if no error), subsequent
arguments - values passed to the reserved slots (created via
this.makeGroup()) of previous step in the order the slots
Same steps container as
Step but it also automatically wraps every single step
with error check and calls the last step if error occurs.
Reserves one slot at the current step. Next step will be called when
all reserved slots of current step will be filled with data or the error occurs.
function(err, data) to fill the slot with data.
Passes one or several synchronous values to the next step.
Reserves slot, creates and returns a group, all results of which will be passed
into the reserved slot as a single array.
slot methods can be called
for created group. If group methods were not called empty array will be passed
into reserved slot.
into cloned repository run
after installtion run
for run tests and generate coverage report run
npm run testAndCover
detailed coverage report will be saved at ./coverage/index.html