Created
November 21, 2013 13:53
-
-
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 file contains 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
/* 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