Created
November 28, 2010 05:01
-
-
Save matthewfl/718609 to your computer and use it in GitHub Desktop.
A async function that I wrote as everything else seemed too complex for what I needed
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
| function async (fun, args) { | |
| if(!fun.length) return; | |
| args = args || []; | |
| var call=fun.shift(); | |
| if(typeof call == "function") call = [ call ]; | |
| var count=call.length; | |
| var n=0; | |
| var ret=[]; | |
| while(call.length) { | |
| call.shift().apply((function (num){ | |
| function r (a) { | |
| ret[num]=a; | |
| if(!--count) | |
| async(fun, ret); | |
| } | |
| for(var c=0;c<args.length;c++) | |
| r[c]=args[c]; | |
| return r; | |
| })(n++)); | |
| } | |
| } | |
| exports.async = async; |
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
| var async = require('./async'); | |
| async([ | |
| [ | |
| function () { | |
| // do something | |
| this("result"); | |
| }, | |
| function () { | |
| // do something | |
| this("result2"); | |
| } | |
| ], | |
| function () { | |
| // this[0] == "result" && this[1] == "result2" | |
| } | |
| ]); |
Author
I have not seen that before, and it is similar, but this was a decently quick hack that I used for jsapp.us
Looked similar in syntax ^_^
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you see https://github.com/creationix/step ?