-
-
Save tobyhede/1116706 to your computer and use it in GitHub Desktop.
Serial/Parallel functions in CoffeeScript
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 characters
| serial = (f) -> | |
| next = -> arr.shift().apply(null, arguments) if arr.length | |
| arr = (v for k, v of f(next)) | |
| 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 | |
| ######################### | |
| # 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment