Created
September 28, 2013 15:17
-
-
Save marcinwyszynski/6743066 to your computer and use it in GitHub Desktop.
Because callbacks are hard.
This file contains hidden or 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
| // Because callbacks are hard. | |
| synchronously = {}; | |
| synchronously.Mutex = function() { | |
| this.locked_ = false; | |
| }; | |
| synchronously.Mutex.prototype.lock = function() { | |
| this.locked_ = true; | |
| }; | |
| synchronously.Mutex.prototype.unlock = function() { | |
| this.locked_ = false; | |
| }; | |
| synchronously.Mutex.prototype.locked = function() { | |
| return !!this.locked_; | |
| }; | |
| synchronously.Do = function(callback) { | |
| var mutex = new Mutex(); | |
| mutex.lock(); | |
| callback.call(mutex); | |
| while (mutex.locked()) {} | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment