lib/promise.js
function(resolvedCallback, rejectedCallback) {
      var promise = new Parse.Promise();

      var wrappedResolvedCallback = function() {
        var result = arguments;
        if (resolvedCallback) {
          result = [resolvedCallback.apply(this, result)];
        }
        if (result.length === 1 && Parse.Promise.is(result[0])) {
          result[0].then(function() {
            promise.resolve.apply(promise, arguments);
          }, function(error) {
            promise.reject(error);
          });
        } else {
          promise.resolve.apply(promise, result);
        }
      };

      var wrappedRejectedCallback = function(error) {
        var result = [];
        if (rejectedCallback) {
          result = [rejectedCallback(error)];
          if (result.length === 1 && Parse.Promise.is(result[0])) {
            result[0].then(function() {
              promise.resolve.apply(promise, arguments);
            }, function(error) {
              promise.reject(error);
            });
          } else {
            // A Promises/A+ compliant implementation would call:
            // promise.resolve.apply(promise, result);
            promise.reject(result[0]);
          }
        } else {
          promise.reject(error);
        }
      };

      if (this._resolved) {
        wrappedResolvedCallback.apply(this, this._result);
      } else if (this._rejected) {
        wrappedRejectedCallback(this._error);
      } else {
        this._resolvedCallbacks.push(wrappedResolvedCallback);
        this._rejectedCallbacks.push(wrappedRejectedCallback);
      }

      return promise;
    }
function(resolvedCallback, rejectedCallback) {
      var promise = new Parse.Promise();

      var wrappedResolvedCallback = function() {
        var result = arguments;
        if (resolvedCallback) {
          result = [resolvedCallback.apply(this, result)];
        }
        if (result.length === 1 && Parse.Promise.is(result[0])) {
          result[0].then(function() {
            promise.resolve.apply(promise, arguments);
          }, function(error) {
            promise.reject(error);
          });
        } else {
          promise.resolve.apply(promise, result);
        }
      };

      var wrappedRejectedCallback = function(error) {
        var result = [];
        if (rejectedCallback) {
          result = [rejectedCallback(error)];
          if (result.length === 1 && Parse.Promise.is(result[0])) {
            result[0].then(function() {
              promise.resolve.apply(promise, arguments);
            }, function(error) {
              promise.reject(error);
            });
          } else {
            // A Promises/A+ compliant implementation would call:
            // promise.resolve.apply(promise, result);
            promise.reject(result[0]);
          }
        } else {
          promise.reject(error);
        }
      };

      if (this._resolved) {
        wrappedResolvedCallback.apply(this, this._result);
      } else if (this._rejected) {
        wrappedRejectedCallback(this._error);
      } else {
        this._resolvedCallbacks.push(wrappedResolvedCallback);
        this._rejectedCallbacks.push(wrappedRejectedCallback);
      }

      return promise;
    }
function ( resolvedCallback , rejectedCallback ) { var promise = new Parse . Promise ( ) ; var wrappedResolvedCallback = function ( ) { var result = arguments ; if ( resolvedCallback ) { result = [ resolvedCallback . apply ( this , result ) ] ; } if ( result . length === 1 && Parse . Promise . is ( result [ 0 ] ) ) { result [ 0 ] . then ( function ( ) { promise . resolve . apply ( promise , arguments ) ; } , function ( error ) { promise . reject ( error ) ; } ) ; } else { promise . resolve . apply ( promise , result ) ; } } ; var wrappedRejectedCallback = function ( error ) { var result = [ ] ; if ( rejectedCallback ) { result = [ rejectedCallback ( error ) ] ; if ( result . length === 1 && Parse . Promise . is ( result [ 0 ] ) ) { result [ 0 ] . then ( function ( ) { promise . resolve . apply ( promise , arguments ) ; } , function ( error ) { promise . reject ( error ) ; } ) ; } else { // A Promises/A+ compliant implementation would call: // promise.resolve.apply(promise, result); promise . reject ( result [ 0 ] ) ; } } else { promise . reject ( error ) ; } } ; if ( this . _resolved ) { wrappedResolvedCallback . apply ( this , this . _result ) ; } else if ( this . _rejected ) { wrappedRejectedCallback ( this . _error ) ; } else { this . _resolvedCallbacks . push ( wrappedResolvedCallback ) ; this . _rejectedCallbacks . push ( wrappedRejectedCallback ) ; } return promise ; }
Adds callbacks to be called when this promise is fulfilled. Returns a new
Promise that will be fulfilled when the callback is complete. It allows
chaining. If the callback itself returns a Promise, then the one returned
by "then" will not be fulfilled until that one returned by the callback
is fulfilled.
@param {Function} resolvedCallback Function that is called when this
Promise is resolved. Once the callback is complete, then the Promise
returned by "then" will also be fulfilled.
@param {Function} rejectedCallback Function that is called when this
Promise is rejected with an error. Once the callback is complete, then
the promise returned by "then" with be resolved successfully. If
rejectedCallback is null, or it returns a rejected Promise, then the
Promise returned by "then" will be rejected with that error.
@return {Parse.Promise} A new Promise that will be fulfilled after this
Promise is fulfilled and either callback has completed. If the callback
returned a Promise, then this Promise will not be fulfilled until that
one is.
Adds callbacks to be called when this promise is fulfilled . Returns a new Promise that will be fulfilled when the callback is complete . It allows chaining . If the callback itself returns a Promise then the one returned by then will not be fulfilled until that one returned by the callback is fulfilled .