Skip to content

Instantly share code, notes, and snippets.

@marcinwyszynski
Created September 28, 2013 15:17
Show Gist options
  • Save marcinwyszynski/6743066 to your computer and use it in GitHub Desktop.
Save marcinwyszynski/6743066 to your computer and use it in GitHub Desktop.
Because callbacks are hard.
// 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