Skip to content

Latest commit

History

21 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

node-block

An async control-flow library for Node.js.
Easily parallel execution and error handling.

Installation

$ npm install node-block

Example

varfs=require('fs');varblock=require('node-block').block;block(function(){fs.readFile('/path/to/file1','utf8',this.async('d1'));fs.readFile('/path/to/file2','utf8',this.async('d2'));},function(){varstr=this.data.d1+this.data.d2;console.log(str);})();

Error handling

Function name is cat and fin.

varfs=require('fs');varblock=require('node-block').block;block(function(){fs.readFile('/path/to/file1','utf8',this.async('d1'));fs.readFile('/path/to/file2','utf8',this.async('d2'));},function(){this.data.d3=this.data.d1+this.data.d2;},functioncat(e){// catchconsole.log(e);throwe;},functionfin(){// finallyconsole.log('fin');// always run})();

Jump to end

call this.end() with return.

varfs=require('fs');varblock=require('node-block').block;block(function(){if(true){returnthis.end();// called with return}},function(){// not run here.},functioncat(e){// catch// when errorless, not run here.},functionfin(){// finallyconsole.log('fin');// always run})();

Callback

sample() is called after fin().

varfs=require('fs');varblock=require('node-block').block;block(function(){fs.readFile('/path/to/file1','utf8',this.async('d1'));},functionfin(){console.log('fin');})(sample);functionsample(err){if(err)throwerr;console.log(this.data.d1);}

Nesting

varfs=require('fs');varblock=require('node-block').block;block(function(){fs.readFile('/path/to/file1','utf8',this.async('d1'));fs.readFile('/path/to/file2','utf8',this.async('d2'));},function(){fs.readFile('/path/to/file3','utf8',this.async('d3'));block(function(){fs.readFile('/path/to/file4','utf8',this.async('e1'));fs.readFile('/path/to/file5','utf8',this.async('e2'));},function(){fs.readFile('/path/to/file6','utf8',this.async('e3'));})(this.async('d4'));},function(){varstr=this.data.d1+this.data.d2+this.data.d3+this.data.d4.e1+this.data.d4.e2+this.data.d4.e3;console.log(str);})();

setTimeout

varblock=require('node-block').block;block(function(){varcb1=this.async('d1');setTimeout(function(){try{cb1(null,'abc');}catch(e){cb1(e);}},1000);},function(){varstr=this.data.d1;console.log(str);// abc})();

License

The MIT License

About

An async control-flow library for Node.js. Easily parallel execution and error handling.

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages