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;
},
¡¡