AJax¼¼Êõ

JavascriptÒÀÀµ×¢Èëѧϰ±Ê¼Ç(5)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2016-03-23 11:00 ÎÒÒªÆÀÂÛ( )

if (noop !== resolver) { try { resolver(function (value) { resolve(promise, value); }, function (reason) { reject(promise, reason); }); } catch (e) { reject(promise, e); } } } FulfillPromise(promise,

  if (noop !== resolver) {
    try {
      resolver(function (value) {
        resolve(promise, value);
      }, function (reason) {
        reject(promise, reason);
      });
    } catch (e) {
      reject(promise, e);
    }
  }
}
FulfillPromise(promise, value)
¼ì²é[[state]]£¬±ØÐëΪpending£¨²»ÊÇpendingµÄ±íʾÒѾ­½âÎö£¬²»ÄÜÖظ´½âÎö£©
¸³Öµ£º[[Result]]=value£¬[[state]]=fulfilled
´¥·¢[[FulfillReactions]]µÄ²Ù×÷
ºÍFulfillPromiseÁªÏµ×î½ôÃܵľÍÊÇResolvePromiseÁË£¬ÕâÀïÎÒÃǸø³öµÄÊÇResolvePromiseµÄʵÏÖ£¬Çø±ðÖ»ÊǶàÁËÖ±½Ó½âÎöPromise¡£

function resolve(promise, value) { 
  // ÒªresolveµÄΪpromise£¨thenµÄcallback·µ»ØµÄÊÇpromise£©
  if (typeof value === 'object'
    && promise.constructor === value.constructor) {
    handleOwnThenable(promise, value);
  }
  // ÒªresolveµÄÊÇÖµ
  else {
    if (promise._state !== PENDING) { return; }

    promise._result = value;
    promise._state = FULFILLED;

    asap(publish, promise);
  }
}

function handleOwnThenable(promise, thenable) { 
  // Èç¹û·µ»ØµÄpromiseÒѾ­Íê³É
  // Ö±½ÓÓøÃpromiseµÄÖµresolve¸¸promise
  if (thenable._state === FULFILLED) {
    resolve(promise, thenable._result);
  } else if (thenable._state === REJECTED) {
    reject(promise, thenable._result);
  }
  // Èç¹û·µ»ØµÄpromiseδÍê³É
  // ÒªµÈ¸ÃpromiseÍê³ÉÔÙresolve¸¸promise
  else {
    subscribe(thenable, undefined, function(value) {
      resolve(promise, value);
    }, function(reason) {
      reject(promise, reason);
    });
  }
}
RejectPromise(promise, reason)
¼ì²é[[state]]£¬±ØÐëΪpending£¨²»ÊÇpendingµÄ±íʾÒѾ­½âÎö£¬²»ÄÜÖظ´½âÎö£©
¸³Öµ£º[[Result]]=reason£¬[[state]]=rejected
´¥·¢[[RejectReactions]]µÄ²Ù×÷
´¥·¢[[FulfillReactions]]ºÍ´¥·¢[[RejectReactions]]ʵ¼Ê¾ÍÊDZéÀúÊý×飬ִÐÐËùÓеĻص÷º¯Êý¡£

function reject(promise, reason) { 
  if (promise._state !== PENDING) { return; }

  promise._state = REJECTED;
  promise._result = reason;

  asap(publish, promise);
}
Promise.prototype.then(onFullfilled, onRejected)
promise=this
н¨resultCapabilityÈýÔª×飬{[[Promise]], [[Resolve]], [[Reject]]}£¨[[Promise]]н¨µÄ£©
fulfillReaction={[[Capabilities]]: resultCapability, [[Handler]]: onFulfilled}
rejectReaction={[[Capabilities]]: resultCapability, [[Handler]]: onRejected}
Èç¹û[[state]]ÊÇpending£ºfulfillReaction¼ÓÈë[[FulfillReactions]]£¬rejectReaction¼ÓÈë[[RejectReactions]]
Èç¹û[[state]]ÊÇfulfilled£ºfulfillReaction¼ÓÈëÖ´ÐжÓÁÐ
Èç¹û[[state]]ÊÇrejected£ºrejectReaction¼ÓÈëÖ´ÐжÓÁÐ
·µ»ØresultCapability.[[Promise]]
ÕâÀï¿ÉÒÔ¿´³ö¹¹Ô캯ÊýºÍthenµÄ¹ØϵÊǺܽôÃܵģ¬Ð½¨µÄpromiseÈç¹ûÊÇÒì²½²Ù×÷£¬ÄÇô״̬¾ÍÊÇpending£¬µ÷ÓÃthenʱ»áн¨×Ópromise£¬²¢ÇÒ½«»Øµ÷²Ù×÷¼ÓÈ븸promiseµÄ[[FulfillReactions]]»ò[[RejectReactions]]µÄÊý×éÀÕâʵ¼Ê¾ÍÊÇ·¢²¼¶©ÔÄģʽ¡£

ËûÃÇÊÇÕâÑùµÄ¹Øϵ£º promise¹¹Ô캯ÊýÓëthenµÄ¹Øϵ

ÎÞÂÛÊÇnew promise»¹Êǵ÷ÓÃthen»òcatch£¬¶¼»áµÃµ½Ò»¸öеÄpromise£¬ÕâЩpromise¶¼»á¶©Ôĸ¸¼¶promiseµÄÍê³Éʼþ£¬¸¸¼¶promiseÍê³ÉÖ®ºó¾Í»áÖ´ÐÐһϵÁеĻص÷²Ù×÷£¬Ò²¾ÍÊÇ·¢²¼¡£

Promise.prototype.catch(onRejected)
thenµÄÓï·¨ÌÇ£ºthen(null, onRejected)
ÏÂÃæ¾ÍÊÇPromiseÔ­ÐÍ£º

Promise.prototype = { 
  constructor: Promise,

  then: function (onFulfillment, onRejection) {
    var parent = this;
    var state = parent._state;

    if (state === FULFILLED && !onFulfillment
        || state === REJECTED && !onRejection) {
        return this;
    }

    var child = new Promise(noop);
    var result = parent._result;

    if (state) {
      var callback = arguments[state - 1];
      asap(function () {
        invokeCallback(state, child, callback, result);
      });
    } else {
      subscribe(parent, child, onFulfillment, onRejection);
    }

    return child;
  },

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Ajax+smarty¼¼ÊõʵÏÖÎÞˢзÖÒ³

    Ajax+smarty¼¼ÊõʵÏÖÎÞˢзÖÒ³

    2016-03-15 15:00

  • JavaScriptѧϰ×ܽáÖ®JS¡¢AJAXÓ¦ÓÃ

    JavaScriptѧϰ×ܽáÖ®JS¡¢AJAXÓ¦ÓÃ

    2016-02-06 15:02

  • ѧϰJavaScriptÉè¼Æģʽ֮´úÀíģʽ

    ѧϰJavaScriptÉè¼Æģʽ֮´úÀíģʽ

    2016-01-25 17:02

  • javascript¡¢jquery¡¢AJAX×ܽá

    javascript¡¢jquery¡¢AJAX×ܽá

    2016-01-25 17:00

ÍøÓѵãÆÀ
Ê