Skip to content

Instantly share code, notes, and snippets.

@alexbosworth
Created August 30, 2011 02:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbosworth/1179973 to your computer and use it in GitHub Desktop.
Save alexbosworth/1179973 to your computer and use it in GitHub Desktop.
Deferred for Node.js
// Deferreds are useful for chaining: doSomething().success(doSomethingElse).failure(stopStuff);
function Deferred() {
this._successCbk = function() {},
this._failureCbk = function() {};
return this;
}
Deferred.prototype.success = function(cbk) {
this._successCbk = cbk;
return this;
};
Deferred.prototype.failure = function(cbk) {
this._failureCbk = cbk;
return this;
};
Deferred.prototype.triggerSuccess = function() {
return this._successCbk.apply(null, arguments);
};
Deferred.prototype.triggerFailure = function() {
return this._failureCbk.apply(null, arguments);
};
exports.Deferred = Deferred;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment