Skip to content

Instantly share code, notes, and snippets.

@matthewfl
Created November 28, 2010 05:01
Show Gist options
  • Select an option

  • Save matthewfl/718609 to your computer and use it in GitHub Desktop.

Select an option

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
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;
var async = require('./async');
async([
[
function () {
// do something
this("result");
},
function () {
// do something
this("result2");
}
],
function () {
// this[0] == "result" && this[1] == "result2"
}
]);
@Marak
Copy link
Copy Markdown

Marak commented Nov 28, 2010

@matthewfl
Copy link
Copy Markdown
Author

I have not seen that before, and it is similar, but this was a decently quick hack that I used for jsapp.us

@Marak
Copy link
Copy Markdown

Marak commented Nov 28, 2010

Looked similar in syntax ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment