Node.js - NextFlow
A simple control-flow library for Node.js targetted towards CoffeeScript developers. It's JavaScript friendly too.
Why?
Take a look at the most prominent JavaScript control flow libraries: Async.js, Step, Seq. If you were to use these libraries in CoffeeScript, your code would be an ugly mess.
Async.js / CoffeeScript
async = require'async' asyncseries -> #first function -> #second function
Step / CoffeeScript
Step = require'step' Step -> #first function -> #second function
Seq / CoffeeScript
Seq = require'seq' Seqseq-> #first function seq-> #second function
Yuck. If you're programming in JavaScript, all of them are very usable solutions. Also, to be fair, they do a lot more than NextFlow. But NextFlow looks much nicer with CoffeeScript programs.
Regarding Async
There's been some comments made towards my criticism of async
. Justifiably so. When, I wrote NextFlow, I wasn't aware of
async's waterfall and object passing capabilities. However, these methods still have their warts. I still believe that NextFlow is a lightweight library compared to async's do everything approach, I also think that NextFlow's syntax is much more pleasing, even for JavaScript development.
Installation
npm install nextflow
Usage
next()
function, pass arguments to next()
if you'd like:
Sequentially, calling the next = require'nextflow' vals = x = 0 next flow = 1: -> valspush1 @next 2: -> valspush2 x = Mathrandom @nextx 3: valspushnum @next 4: -> valspush4 @next 5: -> consolelog vals0#is 1 consolelog vals1#is 2 consolelog vals2#is x consolelog vals3#is 4
Call functions by the label, pass arguments too:
vals = x = 0 next flow = : -> valspush1 @a2 : -> valspush2 x = Mathrandom @a3x : valspushnum @a4 : -> valspush4 @a5 : -> consolelog vals0#is 1 consolelog vals1#is 2 consolelog vals2#is x consolelog vals3#is 4
next()
or call the label:
Call either vals = x = 0y = 0 next flow = : -> valspush1 @a2 : -> valspush2 x = Mathrandom @a3x : valspushnum y = Mathrandom @nexty : valspushnum @a5 : -> consolelog vals0#is 1 consolelog vals1#is 2 consolelog vals2#is x consolelog vals3#is y
Error Handling
Handle errors in one function. Label it error:
, ERROR:
or ErRoR
. Case doesn't matter.
next flow = : consolelog errmessage 1: -> throw 'some error'
Handle errors by passing them as first params of the @next callback:
next flow = : consolelog errmessage #ENOENT, open '/tmp/this_file_hopefully_does_not_exist' 1: -> nonExistentFile = '/tmp/this_file_hopefully_does_not_exist' fsreadFile nonExistentFile@next
Manually call the error function if you want
next flow = : consolelog errmessage #"I feel like calling an error." : -> @error"I feel like calling an error."
JavaScript Friendly
Example pulled from Rock. Also uses BatchFlow.
;
Browser Compatibility
I haven't made this browser compatible just yet, but you can do so with a simple modification of attaching next
to the window
object. Although, I caution you to test thoroughly, as this module depends upon stability of insertion order into the objects. If this is violated, you're going to have problems. It's my understanding that this is not part of the ECMA standard despite most browsers adhering to this.
Read this discussion for more information: http://code.google.com/p/v8/issues/detail?id=164
License
MIT Licensed
Copyright (c) 2012 JP Richardson