Skip to content

Instantly share code, notes, and snippets.

@freddyb
Created November 21, 2013 13:53
Show Gist options
  • Save freddyb/7581901 to your computer and use it in GitHub Desktop.
Save freddyb/7581901 to your computer and use it in GitHub Desktop.
join2Async, could be easily expanded to N-size. looked useful but then didn't solve the problem. dumping here so I may use it when in need. hopefully this makes it easier to find: join queue parallel asynchronous synchronous add flush empty enqueue dequeue
/* this queue-style thing accepts a callback
and calls it when it has two results attached
usage:
j2a = new join2Async(console.log);
setTimeout( 'j2a.addResult("foo")', Math.ceil(Math.random()*10));
setTimeout( 'j2a.addResult("var")', Math.ceil(Math.random()*10));
// enjoy the race and see who's first \o/
*/
function join2Async(cb) {
// join 2 async call and return once *both* are done.
this.firstResult = null;
this.callBack = cb;
}
join2Async.prototype = {
addResult: function add(res) {
if ( this.firstResult === null) {
this.firstResult = res;
}
else { this.callBack( [this.firstResult, res] ) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment