Skip to content

Instantly share code, notes, and snippets.

@laughinghan
Created July 18, 2011 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laughinghan/1090948 to your computer and use it in GitHub Desktop.
Save laughinghan/1090948 to your computer and use it in GitHub Desktop.
Join
/**
* Join
* Joins async callbacks with arguments concatenated.
* Usage:
* var join = new Join;
* getScore (angel, buffy, join());
* getNextMatch (angel, join());
* recordVisitAndGetInfo (angel, buffy, join());
* join.after = function(score, next, vinfo) {
if (this.score > 0.9 && ! this.vinfo.last_visit) {
sendVisitorEmail(angel, buffy);
}
doSomeFinalThings(score, next, vinfo);
* };
*/
function Join() {
var waitingFor = 0, args = [];
return function() {
var argIndex = waitingFor;
waitingFor += 1;
return function() {
args[argIndex] = arguments;
waitingFor -= 1;
if (waitingFor === 0)
join.after.apply(null, args.reduce(__apply_concat, []));
};
};
}
var __apply_concat = Function.apply.bind([].concat);
//equivalent to function(concatedArgs, args){ return [].concat.apply(concatedArgs, args); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment