Skip to content

Instantly share code, notes, and snippets.

@briancavalier
Forked from unscriptable/tiny Promise.js
Created February 11, 2011 17:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save briancavalier/822660 to your computer and use it in GitHub Desktop.
Save briancavalier/822660 to your computer and use it in GitHub Desktop.
An attempt at a more sematic promise API
function Promise () {
this._resolve = [];
this._reject = [];
this._progress = [];
}
Promise.prototype = {
/* This is the "front end" API. */
// Shooting for a more semantic Promise client API.
// Allows chains like:
// promise.then(success).or(fail).using(progress)
then: function (onResolve) {
// capture calls to then()
this._resolve.push(onResolve);
return this;
},
or: function(onReject) {
this._reject.push(onReject);
return this;
},
using: function(onProgress) {
this._progress.push(onProgress);
return this;
},
// Some promise implementations also have a cancel() front end API that
// calls all of the onReject() callbacks (aka a "cancelable promise").
// cancel: function (reason) {},
/* This is the "back end" API. */
// resolve(resolvedValue): The resolve() method is called when a promise
// is resolved (duh). The resolved value (if any) is passed by the resolver
// to this method. All waiting onResolve callbacks are called
// and any future ones are, too, each being passed the resolved value.
resolve: function (val) { this._complete(this._resolve, val, 'then', 'or'); },
// reject(exception): The reject() method is called when a promise cannot
// be resolved. Typically, you'd pass an exception as the single parameter,
// but any other argument, including none at all, is acceptable.
// All waiting and all future onReject callbacks are called when reject()
// is called and are passed the exception parameter.
reject: function (ex) { this._complete(this._reject, ex, 'or', 'then'); },
// Some promises may have a progress handler. The back end API to signal a
// progress "event" has a single parameter. The contents of this parameter
// could be just about anything and is specific to your implementation.
progress: function(statusObject) {
var i=0,
progressFunc;
while(progressFunc = this._progress[i++]) { progressFunc(statusObject); }
},
/* "Private" methods. */
// Passing in the actual array, and which func to make immediate/disable,
// thanks to @unscriptable for the ideas
_complete: function (cbs, arg, immediate, disable) {
// Always disble using(), and also the func that was passed in to be disabled
var noop = this.using = this[disable] =
function() { return this; };
// switch over to sync then() or or()
this[immediate] = function(cb) {cb && cb(arg); return this; }
// disallow multiple calls to resolve or reject
this.resolve = this.reject = this.progress =
function () { throw new Error('Promise already completed.'); };
// complete all waiting (async) then()s
var cb,
i = 0;
while (cb = cbs[i++]) { cb(arg); }
delete this._resolve;
delete this._reject;
delete this._progress;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment