{"id":43350,"date":"2023-12-11T14:59:34","date_gmt":"2023-12-11T06:59:34","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=43350"},"modified":"2023-12-11T14:59:34","modified_gmt":"2023-12-11T06:59:34","slug":"promise%e7%9a%84%e5%ae%9a%e4%b9%89%e4%b8%8e-%e7%9a%84%e5%85%b7%e4%bd%93%e5%ae%9e%e7%8e%b0%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/promise%e7%9a%84%e5%ae%9a%e4%b9%89%e4%b8%8e-%e7%9a%84%e5%85%b7%e4%bd%93%e5%ae%9e%e7%8e%b0%ef%bc%9f\/","title":{"rendered":"promise\u7684\u5b9a\u4e49\u4e0e \u7684\u5177\u4f53\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p>&#8220;`&#8221;                    \u53c2\u8003\u56de\u7b54\uff1a<\/p>\n<p>\u00a0<\/p>\n<p>Promise\u5bf9\u8c61\u662fCommonJS\u5de5\u4f5c\u7ec4\u63d0\u51fa\u7684\u4e00\u79cd\u89c4\u8303\uff0c\u76ee\u7684\u662f\u4e3a\u5f02\u6b65\u7f16\u7a0b\u63d0\u4f9b\u7edf\u4e00\u63a5\u53e3\u3002\u6bcf\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u8fd4\u56de\u4e00\u4e2aPromise\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u6709\u4e00\u4e2athen\u65b9\u6cd5\uff0c\u5141\u8bb8\u6307\u5b9a\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>f1().then(f2);<\/p>\n<p>\u4e00\u4e2apromise\u53ef\u80fd\u6709\u4e09\u79cd\u72b6\u6001\uff1a\u7b49\u5f85\uff08pending\uff09\u3001\u5df2\u5b8c\u6210\uff08resolved\uff0c\u53c8\u79f0fulfilled\uff09\u3001\u5df2\u62d2\u7edd\uff08rejected\uff09\u3002<\/p>\n<p>promise\u5fc5\u987b\u5b9e\u73b0then\u65b9\u6cd5\uff08\u53ef\u4ee5\u8bf4\uff0cthen\u5c31\u662fpromise\u7684\u6838\u5fc3\uff09\uff0c\u800c\u4e14then\u5fc5\u987b\u8fd4\u56de\u4e00\u4e2apromise\uff0c\u540c\u4e00\u4e2apromise\u7684then\u53ef\u4ee5\u8c03\u7528\u591a\u6b21\uff0c\u5e76\u4e14\u56de\u8c03\u7684\u6267\u884c\u987a\u5e8f\u8ddf\u5b83\u4eec\u88ab\u5b9a\u4e49\u65f6\u7684\u987a\u5e8f\u4e00\u81f4\u3002<\/p>\n<p>then\u65b9\u6cd5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u6210\u529f\u65f6\u7684\u56de\u8c03\uff0c\u5728promise\u7531\u201c\u7b49\u5f85\u201d\u6001\u8f6c\u6362\u5230\u201c\u5b8c\u6210\u201d\u6001\u65f6\u8c03\u7528\uff0c\u53e6\u4e00\u4e2a\u662f\u5931\u8d25\u65f6\u7684\u56de\u8c03\uff0c\u5728promise\u7531\u201c\u7b49\u5f85\u201d\u6001\u8f6c\u6362\u5230\u201c\u62d2\u7edd\u201d\u6001\u65f6\u8c03\u7528\u3002\u540c\u65f6\uff0cthen\u53ef\u4ee5\u63a5\u53d7\u53e6\u4e00\u4e2apromise\u4f20\u5165\uff0c\u4e5f\u63a5\u53d7\u4e00\u4e2a\u201c\u7c7bthen\u201d\u7684\u5bf9\u8c61\u6216\u65b9\u6cd5\uff0c\u5373thenable\u5bf9\u8c61\u3002<\/p>\n<p>Promise\u5b9e\u73b0\u5982\u4e0b<\/p>\n<p>&lt;pre&gt;&lt;code&gt;function Promise(fn) {<br \/>\n    var state = &#039;pending&#039;,<br \/>\n    value = null,<br \/>\n    callbacks = [];<br \/>\n    this.then = function (onFulfilled, onRejected) {<br \/>\n    return new Promise(function (resolve, reject) {<br \/>\n    handle({<br \/>\n    onFulfilled: onFulfilled || null,<br \/>\n    onRejected: onRejected || null,<br \/>\n    resolve: resolve,<br \/>\n    reject: reject<br \/>\n    });<br \/>\n    });<br \/>\n    };<br \/>\n    function handle(callback) {<br \/>\n    if (state === &#039;pending&#039;) {<br \/>\n    callbacks.push(callback);<br \/>\n    return;<br \/>\n    }<br \/>\n    var cb = state === &#039;fulfilled&#039; ? callback.onFulfilled : callback.onRejected,<br \/>\n    ret;<br \/>\n    if (cb === null) {<br \/>\n    cb = state === &#039;fulfilled&#039; ? callback.resolve : callback.reject;<br \/>\n    cb(value);<br \/>\n    return;<br \/>\n    }<br \/>\n    ret = cb(value);<br \/>\n    callback.resolve(ret);<br \/>\n    }<br \/>\n    function resolve(newValue) {<br \/>\n    if (newValue &amp;&amp; (typeof newValue === &#039;object&#039; || typeof newValue === &#039;function&#039;)) {<br \/>\n    var then = newValue.then;<br \/>\n    if (typeof then === &#039;function&#039;) {<br \/>\n    then.call(newValue, resolve, reject);<br \/>\n    return;<br \/>\n    }<br \/>\n    }<br \/>\n    state = &#039;fulfilled&#039;;<br \/>\n    value = newValue;<br \/>\n    execute();<br \/>\n    }<br \/>\n    function reject(reason) {<br \/>\n    state = &#039;rejected&#039;;<br \/>\n    value = reason;<br \/>\n    execute();<br \/>\n    }<br \/>\n    function execute() {<br \/>\n    setTimeout(function () {<br \/>\n    callbacks.forEach(function (callback) {<br \/>\n    handle(callback);<br \/>\n    });<br \/>\n    }, 0);<br \/>\n    }<br \/>\n    fn(resolve, reject);<br \/>\n}<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; \u53c2\u8003\u56de\u7b54\uff1a \u00a0 Promise\u5bf9\u8c61\u662fCommonJS\u5de5\u4f5c\u7ec4\u63d0\u51fa\u7684\u4e00\u79cd\u89c4\u8303\uff0c\u76ee\u7684 [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-43350","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43350","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=43350"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43350\/revisions"}],"predecessor-version":[{"id":43351,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/43350\/revisions\/43351"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=43350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=43350"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=43350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}