Easy way to control async flow
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



Original idea: flow-js. You can read about the concept there.

This version is rewritten in coffee using prototypes. It contains some extra features mostly needed for costa.

No js version

There is no js version. Just require('coffee-script') before require('flow-coffee').

Extra features

Custom context

obj =
  someMethod: -> # ...

do new flow(
  context: obj
  blocks: [
    # If context is specified last argument is always a flow instance
    (cb) ->
      @ == obj # true
      cb() # It's callback
    (cb) ->
      do (cb_multi = cb.multi()) -> # multi is also available
        setImmediate -> cb_multi()
    (err, results, cb) -> # ...
    'someMethod' # calls method from context
    (err, cb) -> # other callback


REWIND, MULTI, TIMEOUT are now rewind, multi & setTitmeout. Old names are left for compatibility.

#define() returns just function

f = flow.define( -> )

f.exec()  # removed. no one used i think
f()       # keep it simple

prototype's aftermath

This version works only on engines that support __proto__ (v8 & rhino do).

Now it does mater what context you call multi, setTimeout and other methods.

    multi = @multi
    ... ->
      func multi() # wrong

    state = @
    ... ->
      func state.multi() # ok

Multiple results are stored in the order the @multi() is called, not callbacks

nt = proccess.nextTick
it 'should preserve results order', (cb) ->
  run = []
      do (cb = @multi()) -> nt -> nt -> run.push 1; cb 1
      # this will be ready first:
      do (cb = @multi()) -> nt ->       run.push 2; cb 2
    (err, results)->
      assert.deepEqual run, [2, 1]
      assert.deepEqual (x[0] for x in results), [1, 2]

Function after @multi is called with 2 arguments

It's incompatible with original flow, but I've found it very convenient.

First argument is set to the first error passed to any @multi(). Second is the original results array.

Callback on any error

  -> @ null, 'test'
  (arg) ->
    # strips first (error) arguments for usual callbacks
    assert.equal arg, 'test'
    setImmediate => @ 'error'
  -> # this won't run
).error (err) ->
  # this will run
  # resume flow with @()

Take a look at tests for examples & undocumented features