- 
      
 - 
        
Save tobyhede/1116706 to your computer and use it in GitHub Desktop.  
Revisions
- 
        
Tim Cameron Ryan revised this gist
Jul 18, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 to call when finished) # and an (optional) last function to call with results of each step parallel  - 
        
Tim Cameron Ryan renamed this gist
Jul 18, 2011 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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))  - 
        
Tim Cameron Ryan revised this gist
Jul 18, 2011 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)  - 
        
Tim Cameron Ryan revised this gist
Jul 18, 2011 . 1 changed file with 18 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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('[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 test complete.' ############################## # parallel test with timeouts console.log('Waiting for parallel test...') parallel 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)  - 
        
Tim Cameron Ryan revised this gist
Jul 18, 2011 . 1 changed file with 11 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,18 +4,14 @@ serial = (f) -> next() null 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) last: -> console.log 'serial finished' parallel open: (done) -> fs.open('file', 'w', done) write: (done) -> fs.write({}, 'Apples', done) close: (done) -> fs.close({}, done) (res) -> console.log 'Results:', res  - 
        
Tim Cameron Ryan created this gist
Jul 18, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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