Created
January 13, 2012 03:05
-
-
Save nowelium/1604371 to your computer and use it in GitHub Desktop.
CountdownLatch
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
var CountdownLatch = function (limit){ | |
this.limit = limit; | |
this.count = 0; | |
this.waitBlock = function (){}; | |
}; | |
CountdownLatch.prototype.countDown = function (){ | |
this.count = this.count + 1; | |
if(this.limit <= this.count){ | |
return this.waitBlock(); | |
} | |
}; | |
CountdownLatch.prototype.await = function(callback){ | |
this.waitBlock = callback; | |
}; |
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
var barrier = new CountdownLatch(2); | |
setTimeout(function (){ | |
console.log('work A'); | |
barrier.countDown(); | |
}, 100); | |
setTimeout(function (){ | |
console.log('work B'); | |
barrier.countDown(); | |
}, 200); | |
console.log('wait for all to finish...'); | |
barrier.await(function(){ | |
console.log('done all'); | |
}); | |
==> | |
wait for all to finish... | |
work A | |
work B | |
done all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍 Thanks saved me having to make one!