Better async utilities for node and the browser
setTimeout()for easier CoffeeScript markup
setTimeout(f,0), but grouped
5 thoughtful single-chain 7 order of operations 5 escape callback hell!
Partially backward-compatible with async.js:
asyncseries-> asyncdelay 100@-> asyncdelay 50@->assertcloseTo 100+50sincestart25asyncparallel-> asyncdelay 100@-> asyncdelay 50@->assertcloseTo 100+50+100sincestart25done
But better thanks to several improvements:
asyncserialasserttypeOf next'function'next null'async data' # e.g., fs.readFile(), or jQuery.ajax()parallelassertequal data'async data'asserttypeOf next'function'next nullparallelassertequal data'async data'asserttypeOf next'function'next nullserial->asserttypeOf @'function' # `this` === `next`@ null123456endassertequal errnullassertdeepEqual results 123456done
In fact, way better:
flow =for i in 1..10method = if i%3 then 'parallel' else 'serial' # an overcomplicated display of flexibilityflowmethodasyncdelay 25->consolelog " "nextiflowgoconsolelog 'try this in async.js!'done
It really makes you wonder: how long have we needed a good asynchronous flow control library, and not known it?
Exhibit A: Look familiar to any jQuery.ajax() developers?
called = falseasync: ->#loading.show()called = true:# main logicassertok callednext 'err''result':assertequal 'err''err'#alert err:assert false'success() should not have been called here'#console.log data:assertequal err'err'assertequal result'result'#loading.hide()done
called = falseasynctry->@ 'thrown node cb style'catchcalled = trueassertequal ''+err'Error: thrown node cb style'finallyassertok calledassertequal ''+err'Error: thrown node cb style'asserttypeOf result'undefined'done
Exhibit C: Any Rubists in the audience?
called = falseasyncbegin->@ 'thrown node cb style'rescuecalled = trueassertequal ''+err'Error: thrown node cb style'elseconsolelog 'Else'assert false'else() should not have been called here'ensureassertok calledassertequal ''+err'Error: thrown node cb style'asserttypeOf result'undefined'done
nextTick() users will appreciate the lazy man's grouped blocking serial execution:
asyncpush 'A'setTimeout -> consolelog 'second'; next100asyncpush 'B'setTimeout -> consolelog 'first'; next10asyncpush 'A'setTimeout -> consolelog 'third'; next1# outputs:# first# second# third
These are just a few of all the things it can do.
For the latest examples, review the easy-to-follow ./test/test.coffee.
Or try it immediately in your browser with codepen.
great another high horsed coffeescripter! i just prefer to author in coffee. the .js and .js.min versions are in here too. you can do all the same things; in fact it is partially backward-compatible with async.js but in less lines of .js less bytes i should say; its a minimalist implementation with some improvements.
have you tried pull requests to the async repo? i may if i get positive response but its 100% refactor from ground-up; its not just a pull request. also any snide remarks are meant to encourage spirited but constructive debate. i'm not trying to be divisive. just had a need with a short timeline :) i know a lot of people get used to the way things are...
i could name variables better to assist with minification but its already pretty small. i may do it later though.
potential node.js madness: each series becomes its own cpu thread, each parallel becomes its own gpu thread.
"GPUs have evolved to the point where many real-world applications are easily implemented on them and run significantly faster than on multi-core systems. Future computing architectures will be hybrid systems with parallel-core GPUs working in tandem with multi-core CPUs.' -- Professor Jack Dongarra, Director of the Innovative Computing Laboratory, The University of Tennessee