Skip to content

Instantly share code, notes, and snippets.

@r1cebank
Forked from unscriptable/tiny Promise.js
Created February 29, 2016 18:22
Show Gist options
  • Select an option

  • Save r1cebank/7e2898e5b1664dcb18d5 to your computer and use it in GitHub Desktop.

Select an option

Save r1cebank/7e2898e5b1664dcb18d5 to your computer and use it in GitHub Desktop.

Revisions

  1. John Hann revised this gist Jul 27, 2011. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions tiny Promise.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    // (c) copyright unscriptable.com / John Hann
    // License MIT
    // For more robust promises, see https://github.com/briancavalier/when.js.

    function Promise () {
    this._thens = [];
    }
  2. John Hann revised this gist Feb 7, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions tiny Promise.js
    Original file line number Diff line number Diff line change
    @@ -44,8 +44,8 @@ Promise.prototype = {
    _complete: function (which, arg) {
    // switch over to sync then()
    this.then = which === 'resolve' ?
    function (resolve, reject) { resolve(arg); } :
    function (resolve, reject) { reject(arg); };
    function (resolve, reject) { resolve && resolve(arg); } :
    function (resolve, reject) { reject && reject(arg); };
    // disallow multiple calls to resolve or reject
    this.resolve = this.reject =
    function () { throw new Error('Promise already completed.'); };
  3. John Hann revised this gist Feb 7, 2011. 1 changed file with 31 additions and 3 deletions.
    34 changes: 31 additions & 3 deletions tiny Promise.js
    Original file line number Diff line number Diff line change
    @@ -4,15 +4,43 @@ function Promise () {

    Promise.prototype = {

    then: function (resolve, reject) {
    /* This is the "front end" API. */

    // then(onResolve, onReject): Code waiting for this promise uses the
    // then() method to be notified when the promise is complete. There
    // are two completion callbacks: onReject and onResolve. A more
    // robust promise implementation will also have an onProgress handler.
    then: function (onResolve, onReject) {
    // capture calls to then()
    this._thens.push({ resolve: resolve, reject: reject });
    this._thens.push({ resolve: onResolve, reject: onReject });
    },

    // 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('resolve', val); },

    // 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('reject', ex); },

    // 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 (data) {},

    /* "Private" methods. */

    _complete: function (which, arg) {
    // switch over to sync then()
    this.then = which === 'resolve' ?
    @@ -21,7 +49,7 @@ Promise.prototype = {
    // disallow multiple calls to resolve or reject
    this.resolve = this.reject =
    function () { throw new Error('Promise already completed.'); };
    // complete all async then()s
    // complete all waiting (async) then()s
    var aThen, i = 0;
    while (aThen = this._thens[i++]) { aThen[which] && aThen[which](arg); }
    delete this._thens;
  4. John Hann revised this gist Feb 7, 2011. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions tiny Promise.js
    Original file line number Diff line number Diff line change
    @@ -18,6 +18,9 @@ Promise.prototype = {
    this.then = which === 'resolve' ?
    function (resolve, reject) { resolve(arg); } :
    function (resolve, reject) { reject(arg); };
    // disallow multiple calls to resolve or reject
    this.resolve = this.reject =
    function () { throw new Error('Promise already completed.'); };
    // complete all async then()s
    var aThen, i = 0;
    while (aThen = this._thens[i++]) { aThen[which] && aThen[which](arg); }
  5. John Hann created this gist Feb 7, 2011.
    27 changes: 27 additions & 0 deletions tiny Promise.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    function Promise () {
    this._thens = [];
    }

    Promise.prototype = {

    then: function (resolve, reject) {
    // capture calls to then()
    this._thens.push({ resolve: resolve, reject: reject });
    },

    resolve: function (val) { this._complete('resolve', val); },

    reject: function (ex) { this._complete('reject', ex); },

    _complete: function (which, arg) {
    // switch over to sync then()
    this.then = which === 'resolve' ?
    function (resolve, reject) { resolve(arg); } :
    function (resolve, reject) { reject(arg); };
    // complete all async then()s
    var aThen, i = 0;
    while (aThen = this._thens[i++]) { aThen[which] && aThen[which](arg); }
    delete this._thens;
    }

    };