promise.amd.js 4.12 KB
Newer Older
Yehuda Katz's avatar
Yehuda Katz committed
1 2 3 4 5 6 7
define(
  ["rsvp/config","rsvp/events","exports"],
  function(__dependency1__, __dependency2__, __exports__) {
    "use strict";
    var config = __dependency1__.config;
    var EventTarget = __dependency2__.EventTarget;

8 9 10 11 12 13 14
    function objectOrFunction(x) {
      return isFunction(x) || (typeof x === "object" && x !== null);
    }

    function isFunction(x){
      return typeof x === "function";
    }
Yehuda Katz's avatar
Yehuda Katz committed
15 16

    var Promise = function(resolver) {
17 18
      var promise = this,
      resolved = false;
Yehuda Katz's avatar
Yehuda Katz committed
19 20 21 22 23 24 25 26 27 28

      if (typeof resolver !== 'function') {
        throw new TypeError('You must pass a resolver function as the sole argument to the promise constructor');
      }

      if (!(promise instanceof Promise)) {
        return new Promise(resolver);
      }

      var resolvePromise = function(value) {
29 30
        if (resolved) { return; }
        resolved = true;
Yehuda Katz's avatar
Yehuda Katz committed
31 32 33 34
        resolve(promise, value);
      };

      var rejectPromise = function(value) {
35 36
        if (resolved) { return; }
        resolved = true;
Yehuda Katz's avatar
Yehuda Katz committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        reject(promise, value);
      };

      this.on('promise:resolved', function(event) {
        this.trigger('success', { detail: event.detail });
      }, this);

      this.on('promise:failed', function(event) {
        this.trigger('error', { detail: event.detail });
      }, this);

      resolver(resolvePromise, rejectPromise);
    };

    var invokeCallback = function(type, promise, callback, event) {
52
      var hasCallback = isFunction(callback),
Yehuda Katz's avatar
Yehuda Katz committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
          value, error, succeeded, failed;

      if (hasCallback) {
        try {
          value = callback(event.detail);
          succeeded = true;
        } catch(e) {
          failed = true;
          error = e;
        }
      } else {
        value = event.detail;
        succeeded = true;
      }

68
      if (objectOrFunction(value) && isFunction(value.then)) {
Yehuda Katz's avatar
Yehuda Katz committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        value.then(function(value) {
          resolve(promise, value);
        }, function(error) {
          reject(promise, error);
        });
      } else if (hasCallback && succeeded) {
        resolve(promise, value);
      } else if (failed) {
        reject(promise, error);
      } else if (type === 'resolve') {
        resolve(promise, value);
      } else if (type === 'reject') {
        reject(promise, value);
      }
    };

    Promise.prototype = {
      constructor: Promise,

      then: function(done, fail) {
        var thenPromise = new Promise(function() {});

        if (this.isFulfilled) {
          config.async(function() {
            invokeCallback('resolve', thenPromise, done, { detail: this.fulfillmentValue });
          }, this);
        }

        if (this.isRejected) {
          config.async(function() {
            invokeCallback('reject', thenPromise, fail, { detail: this.rejectedReason });
          }, this);
        }

        this.on('promise:resolved', function(event) {
          invokeCallback('resolve', thenPromise, done, event);
        });

        this.on('promise:failed', function(event) {
          invokeCallback('reject', thenPromise, fail, event);
        });

        return thenPromise;
      }
    };

    EventTarget.mixin(Promise.prototype);

    function resolve(promise, value) {
118 119 120
      if (value && (value === promise) || (objectOrFunction(value) && isFunction(value.promise) && value.promise() === promise)) {
        fulfill(promise, value);
      } else if (objectOrFunction(value) && isFunction(value.then)) {
Yehuda Katz's avatar
Yehuda Katz committed
121
        value.then(function(val) {
122 123 124 125 126
          if (value !== val) {
            resolve(promise, val);
          } else {
            fulfill(promise, val);
          }
Yehuda Katz's avatar
Yehuda Katz committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        }, function(val) {
          reject(promise, val);
        });
      } else {
        fulfill(promise, value);
      }
    }

    function fulfill(promise, value) {
      config.async(function() {
        promise.trigger('promise:resolved', { detail: value });
        promise.isFulfilled = true;
        promise.fulfillmentValue = value;
      });
    }

    function reject(promise, value) {
      config.async(function() {
        promise.trigger('promise:failed', { detail: value });
        promise.isRejected = true;
        promise.rejectedReason = value;
      });
    }

Stefan Penner's avatar
Stefan Penner committed
151

Yehuda Katz's avatar
Yehuda Katz committed
152 153
    __exports__.Promise = Promise;
  });