Skip to content

Instantly share code, notes, and snippets.

@tobyhede
Forked from tcr/async.coffee
Created July 31, 2011 11:07
Show Gist options
  • Save tobyhede/1116706 to your computer and use it in GitHub Desktop.
Save tobyhede/1116706 to your computer and use it in GitHub Desktop.

Revisions

  1. Tim Cameron Ryan revised this gist Jul 18, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion async.coffee
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ serial (next) ->
    console.log('Waiting for parallel test...')

    # parallel accepts a list of steps to execute in any order
    # (each step is passed a "done" continuation which saves the results)
    # (each step is passed a "done" continuation to call when finished)
    # and an (optional) last function to call with results of each step

    parallel
  2. Tim Cameron Ryan renamed this gist Jul 18, 2011. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.txt → async.coffee
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Asynchronous DSL for CoffeeScript

    serial = (f) ->
    next = -> arr.shift().apply(null, arguments) if arr.length
    arr = (v for k, v of f(next))
  3. Tim Cameron Ryan revised this gist Jul 18, 2011. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,10 @@ fs =
    write: (f, _, cb) -> console.log('[fs.write]', f); cb(0, f)
    close: (f, cb) -> console.log('[fs.close]', f); cb(0, f)

    # serial accepts a function with one "next" continuation,
    # which returns a list of steps to execute in order
    # you can label your steps anything, but numbers look good:

    serial (next) ->
    1: -> fs.open('file', 'w', next)
    2: (err, f) -> fs.write(f, 'Apples', (a...) -> next(a..., f))
    @@ -33,6 +37,10 @@ serial (next) ->

    console.log('Waiting for parallel test...')

    # parallel accepts a list of steps to execute in any order
    # (each step is passed a "done" continuation which saves the results)
    # and an (optional) last function to call with results of each step

    parallel
    A: (done) -> setTimeout((-> done(new Date)), 1000)
    B: (done) -> setTimeout((-> done(new Date)), 3000)
  4. Tim Cameron Ryan revised this gist Jul 18, 2011. 1 changed file with 18 additions and 12 deletions.
    30 changes: 18 additions & 12 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -14,21 +14,27 @@ parallel = (f, after = ->) ->
    if not --arrc then after(res)
    null

    #################
    #########################
    # serial test w/ mock fs

    fs =
    open: (_, _, cb) -> console.log('open'); cb(0, {a:5})
    write: (f, _, cb) -> console.log('write', f); cb(0, f)
    close: (f, cb) -> console.log('close', f); cb(0, f)
    open: (_, _, cb) -> console.log('[fs.open]'); cb(0, {a_fake: 'file object'})
    write: (f, _, cb) -> console.log('[fs.write]', f); cb(0, f)
    close: (f, cb) -> console.log('[fs.close]', f); cb(0, f)

    serial (next) ->
    1: -> fs.open('file', 'w', next)
    2: (err, f) -> fs.write(f, 'Apples', (a...) -> next(a..., f))
    3: (err, written, f) -> fs.close(f, next)
    last: -> console.log 'serial finished'
    1: -> fs.open('file', 'w', next)
    2: (err, f) -> fs.write(f, 'Apples', (a...) -> next(a..., f))
    3: (err, written, f) -> fs.close(f, next)
    last: -> console.log 'Serial test complete.'

    ##############################
    # parallel test with timeouts

    console.log('Waiting for parallel test...')

    parallel
    open: (done) -> fs.open('file', 'w', done)
    write: (done) -> fs.write({}, 'Apples', done)
    close: (done) -> fs.close({}, done)
    (res) -> console.log 'Results:', res
    A: (done) -> setTimeout((-> done(new Date)), 1000)
    B: (done) -> setTimeout((-> done(new Date)), 3000)
    C: (done) -> setTimeout((-> done(new Date)), 2000)
    (res) -> console.log 'Parallel results:', JSON.stringify(res)
  5. Tim Cameron Ryan revised this gist Jul 18, 2011. 1 changed file with 11 additions and 15 deletions.
    26 changes: 11 additions & 15 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -4,18 +4,14 @@ serial = (f) ->
    next()
    null

    parallel = (f) ->
    res = {}; arrc = 0; after = ->
    for k, v of f()
    if k == "after"
    after = v
    if not arrc then after(res)
    else
    arrc++
    do (k, v) ->
    v (args...) ->
    res[k] = args
    if not --arrc then after(res)
    parallel = (f, after = ->) ->
    res = {}; arrc = 0
    arrc++ for k, v of f
    for k, v of f
    do (k, v) ->
    v (args...) ->
    res[k] = args
    if not --arrc then after(res)
    null

    #################
    @@ -29,10 +25,10 @@ serial (next) ->
    1: -> fs.open('file', 'w', next)
    2: (err, f) -> fs.write(f, 'Apples', (a...) -> next(a..., f))
    3: (err, written, f) -> fs.close(f, next)
    after: -> console.log 'serial finished'
    last: -> console.log 'serial finished'

    parallel ->
    parallel
    open: (done) -> fs.open('file', 'w', done)
    write: (done) -> fs.write({}, 'Apples', done)
    close: (done) -> fs.close({}, done)
    after: (res) -> console.log 'Results:', res
    (res) -> console.log 'Results:', res
  6. Tim Cameron Ryan created this gist Jul 18, 2011.
    38 changes: 38 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    serial = (f) ->
    next = -> arr.shift().apply(null, arguments) if arr.length
    arr = (v for k, v of f(next))
    next()
    null

    parallel = (f) ->
    res = {}; arrc = 0; after = ->
    for k, v of f()
    if k == "after"
    after = v
    if not arrc then after(res)
    else
    arrc++
    do (k, v) ->
    v (args...) ->
    res[k] = args
    if not --arrc then after(res)
    null

    #################

    fs =
    open: (_, _, cb) -> console.log('open'); cb(0, {a:5})
    write: (f, _, cb) -> console.log('write', f); cb(0, f)
    close: (f, cb) -> console.log('close', f); cb(0, f)

    serial (next) ->
    1: -> fs.open('file', 'w', next)
    2: (err, f) -> fs.write(f, 'Apples', (a...) -> next(a..., f))
    3: (err, written, f) -> fs.close(f, next)
    after: -> console.log 'serial finished'

    parallel ->
    open: (done) -> fs.open('file', 'w', done)
    write: (done) -> fs.write({}, 'Apples', done)
    close: (done) -> fs.close({}, done)
    after: (res) -> console.log 'Results:', res