{"id":206200,"date":"2020-12-09T10:45:43","date_gmt":"2020-12-09T02:45:43","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=206200"},"modified":"2020-12-02T10:21:48","modified_gmt":"2020-12-02T02:21:48","slug":"promise-start-here","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/promise-start-here.html","title":{"rendered":"\u6df1\u5165\u7406\u89e3Promise\u4ece\u8fd9\u91cc\u5f00\u59cb"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6211\u4eec\u90fd\u77e5\u9053 JavaScript \u7684\u4ee3\u7801\u6267\u884c\u7684\u65f6\u5019\u662f\u8dd1\u5728\u5355\u7ebf\u7a0b\u4e0a\u7684\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u53ea\u80fd\u6309\u7167\u4ee3\u7801\u7684\u51fa\u73b0\u987a\u5e8f\uff0c\u4ece\u4e0a\u5230\u4e0b\u4e00\u884c\u4e00\u884c\u7684\u6267\u884c\uff0c\u4f46\u662f\u9047\u5230\u4e86\u5f02\u6b65\u7684\u884c\u4e3a\uff0c\u6bd4\u5982\u5b9a\u65f6\u5668\uff08\u4e00\u5b9a\u65f6\u95f4\u4e4b\u540e\u624d\u53bb\u6267\u884c\uff09\uff0c\u90a3\u5c31\u9700\u8981\u7b49\u540c\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6210\u540e\u7684\u4e00\u6bb5\u65f6\u95f4\u91cc\u518d\u53bb\u6267\u884c\u5f02\u6b65\u4ee3\u7801\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4ece\u5f02\u6b65\u7f16\u7a0b\u8bf4\u8d77<\/strong><\/div>\n

\u6211\u4eec\u90fd\u77e5\u9053 JavaScript \u7684\u4ee3\u7801\u6267\u884c\u7684\u65f6\u5019\u662f\u8dd1\u5728\u5355\u7ebf\u7a0b\u4e0a\u7684\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u53ea\u80fd\u6309\u7167\u4ee3\u7801\u7684\u51fa\u73b0\u987a\u5e8f\uff0c\u4ece\u4e0a\u5230\u4e0b\u4e00\u884c\u4e00\u884c\u7684\u6267\u884c\uff0c\u4f46\u662f\u9047\u5230\u4e86\u5f02\u6b65\u7684\u884c\u4e3a\uff0c\u6bd4\u5982\u5b9a\u65f6\u5668(\u4e00\u5b9a\u65f6\u95f4\u4e4b\u540e\u624d\u53bb\u6267\u884c)\uff0c\u90a3\u5c31\u9700\u8981\u7b49\u540c\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6210\u540e\u7684\u4e00\u6bb5\u65f6\u95f4\u91cc\u518d\u53bb\u6267\u884c\u5f02\u6b65\u4ee3\u7801\u3002<\/p>\n

\u5bf9\u4e8e\u540c\u6b65\u884c\u4e3a\uff0c\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u80fd\u591f\u5f88\u6e05\u695a\u7684\u77e5\u9053\u6bcf\u4e00\u884c\u4f1a\u53d1\u751f\u4ec0\u4e48\uff0c\u8fd9\u662f\u56e0\u4e3a\u540e\u9762\u7684\u6307\u4ee4\u603b\u662f\u7b49\u5230\u524d\u9762\u7684\u6307\u4ee4\u6267\u884c\u5b8c\u6210\u540e\u624d\u53bb\u6267\u884c\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684\u7b2c\u4e8c\u884c\u91cc\u7684\u53d8\u91cf x \u5728\u5185\u5b58\u91cc\u5df2\u7ecf\u662f\u5b9a\u4e49\u8fc7\u7684\u3002<\/p>\n

\r\nlet x = 10;let y = x + 5; \r\n<\/pre>\n

\u4f46\u662f\u5bf9\u4e8e\u5f02\u6b65\u4ee3\u7801\uff0c\u6211\u4eec\u5c31\u4e0d\u597d\u63a8\u65ad\u5230\u5e95\u4ec0\u4e48\u65f6\u5019\u4f1a\u6267\u884c\u5b8c\u6210\u4e86\u3002\u6bd4\u5982\u4e3e\u4e00\u4e2a\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u53bb\u52a8\u6001\u52a0\u8f7d\u67d0\u4e2a\u811a\u672c\uff0c\u4f1a\u8fd9\u6837\u505a\uff1a<\/p>\n

\r\nfunction loadScript(src) { \r\n    let script = document.createElement('script') \r\n    script.src = src \r\n    document.head.append(script) \r\n} \r\n<\/pre>\n

\u8fd9\u4e2a\u811a\u672c\u52a0\u8f7d\u5b8c\u6210\u7684\u65f6\u5019\u4f1a\u53bb\u6267\u884c\u5b9a\u4e49\u5728\u811a\u672c\u91cc\u7684\u4e00\u4e9b\u51fd\u6570\uff0c\u6bd4\u5982\u521d\u59cb\u5316\u51fd\u6570 init\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u4f1a\u8fd9\u6837\u5199\uff1a<\/p>\n

\r\nfunction loadScript(src) { \r\n    let script = document.createElement('script') \r\n    script.src = src \r\n    document.head.append(script) \r\n} \r\nloadScript('.\/js\/script.js') \r\n<\/pre>\n

init() \/\/ \u5b9a\u4e49\u5728 .\/js\/script.js \u91cc\u7684\u51fd\u6570
\n\u4f46\u662f\u5b9e\u9645\u6267\u884c\u540e\u5374\u53d1\u73b0\uff0c\u8fd9\u6837\u6839\u672c\u4e0d\u884c\uff0c\u56e0\u4e3a\u52a0\u8f7d\u811a\u672c\u662f\u9700\u8981\u82b1\u65f6\u95f4\u7684\uff0c\u662f\u4e00\u4e2a\u5f02\u6b65\u7684\u884c\u4e3a\uff0c\u6d4f\u89c8\u5668\u6267\u884c JavaScript \u7684\u65f6\u5019\u5e76\u4e0d\u4f1a\u7b49\u5230\u811a\u672c\u52a0\u8f7d\u5b8c\u6210\u7684\u65f6\u5019\u518d\u53bb\u8c03\u7528 init \u51fd\u6570\u3002<\/p>\n

\u4ee5\u5f80\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u5f02\u6b65\u7f16\u7a0b\u7684\u505a\u6cd5\u901a\u5e38\u5c31\u662f\u901a\u8fc7\u7ed9\u51fd\u6570\u4f20\u9012\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\uff0c\u4e0a\u9762\u90a3\u4e2a\u4f8b\u5b50\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n

\r\nfunction loadScript(src, success, fail) { \r\n    let script = document.createElement('script') \r\n    script.src = src \r\n    script.onload = success \r\n    script.onerror = fail \r\n    document.head.append(script) \r\n} \r\nloadScript('.\/js\/script.js', success, fail) \r\nfunction success() { \r\n    console.log('success') \r\n    init()  \/\/ \u5b9a\u4e49\u5728 .\/js\/script.js \u4e2d\u7684\u51fd\u6570 \r\n} \r\nfunction fail() { \r\n    console.log('fail') \r\n} \r\n<\/pre>\n

\u4e0a\u9762\u8fd9\u6837\u505a\u80fd\u591f\u4fdd\u8bc1\u5728\u811a\u672c\u52a0\u8f7d\u5b8c\u6210\u7684\u65f6\u5019\uff0c\u518d\u53bb\u6267\u884c\u811a\u672c\u91cc\u7684\u51fd\u6570\u3002\u4f46\u662f\u591a\u8003\u8651\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c success \u91cc\u53c8\u9700\u8981\u52a0\u8f7d\u522b\u7684 js \u6587\u4ef6\u5462\uff0c\u90a3\u5c82\u4e0d\u662f\u9700\u8981\u591a\u5c42\u5d4c\u5957\u4e86\u3002\u662f\u7684\uff0c\u8fd9\u6837\u7684\u591a\u5c42\u5d4c\u5957\u4f1a\u4f7f\u5f97\u4ee3\u7801\u5c42\u6b21\u53d8\u5f97\u66f4\u52a0\u6df1\u5165\uff0c\u96be\u4ee5\u9605\u8bfb\u4ee5\u53ca\u540e\u671f\u7ef4\u62a4\u6210\u672c\u975e\u5e38\u9ad8\uff0c\u5c24\u5176\u662f\u5f53\u91cc\u9762\u52a0\u4e0a\u4e86\u5f88\u591a\u7684\u5224\u65ad\u903b\u8f91\u7684\u65f6\u5019\u60c5\u51b5\u4f1a\u66f4\u52a0\u7cdf\u7cd5\uff0c\u8fd9\u5c31\u662f\u6240\u8c13\u7684 \u201c\u56de\u8c03\u5730\u72f1\u201d\uff0c\u4e14\u53c8\u56e0\u4e3a\u5b83\u7684\u4ee3\u7801\u5f62\u72b6\u5f88\u50cf\u8eba\u7740\u7684\u91d1\u5b57\u5854\uff0c\u6240\u4ee5\u6709\u7684\u4eba\u4e5f\u559c\u6b22\u53eb\u5b83 \u201c\u5669\u8fd0\u91d1\u5b57\u5854\u201d\u3002<\/p>\n

\u800c\u4e3a\u4e86\u907f\u514d\u8fd9\u7c7b \u201c\u56de\u8c03\u5730\u72f1\u201d \u95ee\u9898\uff0c\u76ee\u524d\u6700\u597d\u7684\u505a\u6cd5\u4e4b\u4e00\u5c31\u662f\u4f7f\u7528 Promise\u3002<\/p>\n

Promise\u6b63\u7bc7<\/strong><\/div>\n

\u4f7f\u7528 Promise \u53ef\u4ee5\u5f88\u597d\u7684\u89e3\u51b3\u4e0a\u9762\u63d0\u5230\u7684 \u201c\u56de\u8c03\u5730\u72f1\u201d \u95ee\u9898\uff0c\u76f4\u63a5\u6765\u770b\u7ed3\u679c\uff1a<\/p>\n

\r\nfunction loadScript(src) { \r\n    return new Promise(function(resolve, reject) { \r\n        let script = document.createElement('script'); \r\n        script.src = src; \r\n        script.onload = () => resolve(script); \r\n        script.onerror = () => reject(new Error(`Script load error for ${src}`)); \r\n        document.head.append(script); \r\n    }); \r\n} \r\nloadScript('.\/scripts.js').then(res => { \r\n    console.log('success', res); \r\n    init() \r\n}).catch(err => { \r\n    console.log(err); \r\n}) \r\n<\/pre>\n

\u8fd9\u91cc\u901a\u8fc7\u4f7f\u7528 Promise \u5b9e\u4f8b\u7684 then \u548c catch \u51fd\u6570\u5c06\u591a\u5c42\u5d4c\u5957\u7684\u4ee3\u7801\u6539\u6210\u4e86\u540c\u6b65\u5904\u7406\u6d41\u7a0b\uff0c\u770b\u8d77\u6765\u6548\u679c\u8fd8\u662f\u4e0d\u9519\u7684\uff0c\u90a3\u4ec0\u4e48\u662f Promise \u5462?<\/p>\n

Promise \u9996\u5148\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5b83\u901a\u5e38\u7528\u4e8e\u63cf\u8ff0\u73b0\u5728\u5f00\u59cb\u6267\u884c\uff0c\u4e00\u6bb5\u65f6\u95f4\u540e\u624d\u80fd\u83b7\u5f97\u7ed3\u679c\u7684\u884c\u4e3a(\u5f02\u6b65\u884c\u4e3a)\uff0c\u5185\u90e8\u4fdd\u5b58\u4e86\u8be5\u5f02\u6b65\u884c\u4e3a\u7684\u7ed3\u679c\u3002\u7136\u540e\uff0c\u5b83\u8fd8\u662f\u4e00\u4e2a\u6709\u72b6\u6001\u7684\u5bf9\u8c61\uff1a
\npending\uff1a\u5f85\u5b9a
\nfulfilled\uff1a\u5151\u73b0\uff0c\u6709\u65f6\u5019\u4e5f\u53eb\u89e3\u51b3(resolved)
\nrejected\uff1a\u62d2\u7edd
\n\u4e00\u4e2a Promise \u53ea\u6709\u8fd9 3 \u79cd\u72b6\u6001\uff0c\u4e14\u72b6\u6001\u7684\u8f6c\u6362\u8fc7\u7a0b\u6709\u4e14\u4ec5\u6709 2 \u79cd\uff1a
\npending \u5230 fulfilled
\npending \u5230 rejected
\n\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u7684 Promise \u5bf9\u8c61\u6784\u9020\u5668\u6765\u521b\u5efa\u4e00\u4e2a Promise\uff1a<\/p>\n

\r\nlet promise = new Promise((resolve, reject) => {}) \r\n<\/pre>\n

\u4f20\u9012\u7ed9 new Promise \u7684\u662f executor \u6267\u884c\u5668\u3002\u5f53 Promise \u88ab\u521b\u5efa\u7684\u65f6\u5019\uff0cexecutor \u4f1a\u7acb\u5373\u540c\u6b65\u6267\u884c\u3002executor \u51fd\u6570\u91cc\u901a\u5e38\u505a\u4e86 2 \u4ef6\u4e8b\u60c5\uff1a\u521d\u59cb\u5316\u4e00\u4e2a\u5f02\u6b65\u884c\u4e3a\u548c\u63a7\u5236\u72b6\u6001\u7684\u6700\u7ec8\u8f6c\u6362\u3002<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    setTimeout(() => { \r\n        resolve() \r\n    }, 1000) \r\n}) \r\n<\/pre>\n

\u5982\u4e0a\u4ee3\u7801\u6240\u793a\uff0csetTimeout \u51fd\u6570\u7528\u6765\u63cf\u8ff0\u4e00\u4e2a\u5f02\u6b65\u884c\u4e3a\uff0c\u800c resolve \u7528\u6765\u6539\u53d8\u72b6\u6001\u3002executor \u51fd\u6570\u5305\u542b 2 \u4e2a\u53c2\u6570\uff0c\u4ed6\u4eec\u90fd\u662f\u56de\u8c03\u51fd\u6570\uff0c\u7528\u4e8e\u63a7\u5236 Promise \u7684\u72b6\u6001\u8f6c\u6362\uff1a<\/p>\n

resolve\uff1a\u7528\u6765\u5c06\u72b6\u6001 pending \u8f6c\u6362\u6210 fulfilled
\nreject\uff1a\u7528\u6765\u5c06\u72b6\u6001 pending \u8f6c\u6362\u6210 rejected
\n\u4e00\u4e2a Promise \u7684\u72b6\u6001\u4e00\u65e6\u88ab\u8f6c\u6362\u8fc7\uff0c\u5219\u65e0\u6cd5\u518d\u53d8\u66f4\uff1a<\/p>\n

\r\nlet p = new Promise((resolve, reject) => { \r\n    setTimeout(() => { \r\n        resolve('\u7b2c\u4e00\u6b21 resolve') \r\n        resolve('\u7b2c\u4e8c\u6b21 resolve')  \/\/ \u5c06\u88ab\u5ffd\u7565 \r\n        reject('\u7b2c\u4e00\u6b21 reject')  \/\/ \u5c06\u88ab\u5ffd\u7565 \r\n    }, 0) \r\n}) \r\nsetTimeout(console.log, 1000, p)  \/\/ Promise {: \"\u7b2c\u4e00\u6b21 resolve\"} \r\n<\/fulfilled><\/pre>\n

\u53ef\u4ee5\u770b\u5230\u6267\u884c\u4e86 2 \u6b21 resolve \u51fd\u6570\u548c 1 \u6b21 reject \u51fd\u6570\uff0c\u4f46\u662f promise \u7684\u6700\u7ec8\u7ed3\u679c\u662f\u53d6\u7684\u7b2c\u4e00\u6b21 resolve \u7684\u7ed3\u679c\uff0c\u5370\u8bc1\u4e86\u4e0a\u9762\u7684\u7ed3\u8bba\u3002<\/p>\n

\u7531 new Promise \u6784\u9020\u5668\u8fd4\u56de\u7684 Promise \u5bf9\u8c61\u5177\u6709\u5982\u4e0b\u5185\u90e8\u5c5e\u6027\uff1a<\/p>\n

PromiseState\uff1a\u6700\u521d\u662f pending\uff0cresolve \u88ab\u8c03\u7528\u7684\u65f6\u5019\u53d8\u4e3a fulfilled\uff0c\u6216\u8005 reject \u88ab\u8c03\u7528\u65f6\u4f1a\u53d8\u4e3a rejected;
\nPromiseResult\uff1a\u6700\u521d\u662f undefined\uff0cresolve(value) \u88ab\u8c03\u7528\u65f6\u53d8\u4e3a value\uff0c\u6216\u8005\u5728 reject(error) \u88ab\u8c03\u7528\u65f6\u53d8\u4e3a error\u3002
\n\u6bd4\u5982\u4e0a\u9762\u4f8b\u5b50\u4e2d\u6253\u5370\u51fa\u6765\u7684 Promise \u5bf9\u8c61\u7ed3\u679c\u4e2d\uff0cfulfilled \u662f\u5176\u5185\u90e8\u7684 PromiseState\uff0c\u800c \u201c\u7b2c\u4e00\u6b21 resolve\u201d \u662f\u5176 PromiseResult\u3002<\/p>\n

\r\n\/\/ Promise {: \"\u7b2c\u4e00\u6b21 resolve\"} <\/fulfilled><\/pre>\n
Promise\u5b9e\u4f8b\u65b9\u6cd5<\/strong><\/div>\n
Promise.prototype.then()<\/strong><\/span><\/div>\n

Promise.prototype.then() \u5c06\u7528\u4e8e\u4e3a Promise \u5b9e\u4f8b\u6dfb\u52a0\u5904\u7406\u7a0b\u5e8f\u7684\u51fd\u6570\u3002\u5b83\u63a5\u53d7 2 \u4e2a\u53ef\u9009\u7684\u53c2\u6570\uff1a<\/p>\n

\r\nonResolved\uff1a\u72b6\u6001\u7531 pending \u8f6c\u6362\u6210 fulfilled \u65f6\u6267\u884c;\r\nonRejected\uff1a\u72b6\u6001\u7531 pending \u8f6c\u6362\u6210 rejected \u65f6\u6267\u884c\u3002\r\n<\/pre>\n

\u5b83\u53ef\u4ee5\u5199\u6210\u8fd9\u6837\uff1a<\/p>\n

\r\nfunction onResolved(res) { \r\n    console.log('resolved' + res)  \/\/ resolved3 \r\n} \r\nfunction onRejected(err) { \r\n    console.log('rejected' + err) \r\n} \r\nnew Promise((resolve, reject) => { \r\n    resolve(3) \r\n}).then(onResolved, onRejected) \r\n<\/pre>\n

\u6216\u8005\u5199\u6210\u66f4\u7b80\u5355\u7684\u65b9\u5f0f\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    resolve(3) \r\n}).then(res => { \r\n    console.log('resolved' + res)  \/\/ resolved3 \r\n}, err => { \r\n    console.log('rejected' + err) \r\n}) \r\n<\/pre>\n

\u56e0\u4e3a\u72b6\u6001\u7684\u53d8\u5316\u53ea\u6709 2 \u79cd\uff0c\u6240\u4ee5 onResolved \u548c onRejected \u5728\u6267\u884c\u7684\u65f6\u5019\u5fc5\u5b9a\u662f\u4e92\u65a5\u3002<\/p>\n

\u4e0a\u9762\u4ecb\u7ecd\u5230\u4e86 then() \u7684\u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u5f53\u53ea\u6709 onResolved \u7684\u65f6\u5019\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    resolve() \r\n}).then(res => {}) \r\n<\/pre>\n

\u5f53\u53c2\u6570\u53ea\u6709 onRejected \u7684\u65f6\u5019\uff0c\u9700\u8981\u628a\u7b2c\u4e00\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3a null\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    reject() \r\n}).then(null, err => {}) \r\n<\/pre>\n

\u5982\u679c\u7ed9 then() \u51fd\u6570\u4f20\u9012\u6765\u4e86\u975e\u51fd\u6570\u53c2\u6570\uff0c\u5219\u4f1a\u9ed8\u8ba4\u5ffd\u7565\u3002<\/p>\n

Promise.prototype.catch()<\/strong><\/span><\/div>\n

Promise.prototype.catch() \u7528\u4e8e\u7ed9 Promise \u5bf9\u8c61\u6dfb\u52a0\u62d2\u7edd\u5904\u7406\u7a0b\u5e8f\u3002\u53ea\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff1aonRejected \u51fd\u6570\u3002\u5b9e\u9645\u4e0a\uff0c\u4e0b\u9762\u8fd9\u4e24\u79cd\u5199\u6cd5\u662f\u7b49\u6548\u7684\uff1a<\/p>\n

\r\nfunction onRejected(err){} \r\nnew Promise((resolve, reject) => { \r\n    reject() \r\n}).catch(onRejected) \r\nnew Promise((resolve, reject) => { \r\n    reject() \r\n}).then(null, onRejected) \r\nPromise.prototype.finally()\r\n\r\nPromise.prototype.finally() \u7528\u4e8e\u7ed9 Promise \u5bf9\u8c61\u6dfb\u52a0 onFinally \u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u4e3b\u8981\u662f\u505a\u4e00\u4e9b\u6e05\u7406\u7684\u5de5\u4f5c\uff0c\u53ea\u6709\u72b6\u6001\u53d8\u5316\u7684\u65f6\u5019\u624d\u4f1a\u6267\u884c\u8be5 onFinally \u51fd\u6570\u3002\r\n\r\nfunction onFinally() { \r\n    console.log(888)  \/\/ \u5e76\u4e0d\u4f1a\u6267\u884c   \r\n} \r\nnew Promise((resolve, reject) => { \r\n     \r\n}).finally(onFinally) \r\n<\/pre>\n

\u56e0\u4e3a onFinally \u51fd\u6570\u662f\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\u7684\uff0c\u6240\u4ee5\u5728\u5176\u5185\u90e8\u5176\u5b9e\u5e76\u4e0d\u77e5\u9053\u8be5 Promise \u7684\u72b6\u6001\u662f\u600e\u4e48\u6837\u7684\u3002<\/p>\n

\u94fe\u5f0f\u8c03\u7528<\/strong><\/div>\n

\u94fe\u5f0f\u8c03\u7528\u91cc\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5f88\u591a\uff0c\u6211\u4eec\u4e0d\u59a8\u5148\u770b\u770b\u4e0b\u9762\u8fd9\u9053\u9898\uff0c\u4f60\u80fd\u6b63\u786e\u8f93\u51fa\u5176\u6253\u5370\u987a\u5e8f\u561b?<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    resolve() \r\n}).then(() => { \r\n    console.log('A') \r\n    new Promise((resolve, reject) => { \r\n        resolve() \r\n    }).then(() => { \r\n        console.log('B') \r\n    }).then(() => { \r\n        console.log('C') \r\n    }) \r\n}).then(() => { \r\n    console.log('D') \r\n}) \r\n<\/pre>\n

\u8fd9\u91cc\u6211\u4e0d\u7ed9\u51fa\u7b54\u6848\uff0c\u5e0c\u671b\u4f60\u80fd\u52a8\u624b\u6572\u4e00\u6572\u4ee3\u7801\uff0c\u7136\u540e\u601d\u8003\u4e0b\u4e3a\u4ec0\u4e48?\u5bb9\u6211\u8bb2\u5b8c\u8fd9\u90e8\u5206\u77e5\u8bc6\uff0c\u76f8\u4fe1\u4f60\u80fd\u81ea\u5df1\u7406\u89e3\u5176\u4e2d\u7f18\u7531\u3002<\/p>\n

\u4ece\u4e0a\u9762\u8fd9\u4e32\u4ee3\u7801\u91cc\uff0c\u6211\u4eec\u770b\u5230 new Promise \u540e\u9762\u63a5\u4e86\u5f88\u591a\u7684 .then() \u5904\u7406\u7a0b\u5e8f\uff0c\u8fd9\u4e2a\u5176\u5b9e\u5c31\u662f Promise \u7684\u94fe\u5f0f\u8c03\u7528\uff0c\u90a3\u5b83\u4e3a\u4ec0\u4e48\u80fd\u94fe\u5f0f\u8c03\u7528\u5462?<\/p>\n

\u57fa\u4e8eonResolved\u751f\u6210\u4e00\u4e2a\u65b0\u7684Promise<\/strong><\/div>\n

\u56e0\u4e3a Promise.prototype.then() \u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Promise\uff0c\u6765\u770b\u4e0b\uff1a<\/p>\n

\r\nlet p1 = new Promise((resolve, reject) => { \r\n    resolve(3) \r\n}) \r\nlet p2 = p1.then(() => 6) \r\nsetTimeout(console.log, 0, p1)  \/\/ Promise {: 3} \r\nsetTimeout(console.log, 0, p2)  \/\/ Promise {<\/fulfilled>: 6} \r\n<\/fulfilled><\/pre>\n

\u53ef\u4ee5\u770b\u5230 p1 \u548c p2 \u7684\u5185\u90e8 PromiseResult \u662f\u4e0d\u4e00\u6837\u7684\uff0c\u8bf4\u660e p2 \u662f\u4e00\u4e2a\u65b0\u7684 Promise \u5b9e\u4f8b\u3002<\/p>\n

\u65b0\u4ea7\u751f\u7684 Promise \u4f1a\u57fa\u4e8e onResolved \u7684\u8fd4\u56de\u503c\u8fdb\u884c\u6784\u5efa\uff0c\u6784\u5efa\u7684\u65f6\u5019\u5176\u5b9e\u662f\u628a\u8fd4\u56de\u503c\u4f20\u9012\u7ed9 Promise.resolve() \u751f\u6210\u7684\u65b0\u5b9e\u4f8b\uff0c\u6bd4\u5982\u4e0a\u9762\u90a3\u4e32\u4ee3\u7801\u91cc p1.then(() => 6) \u8fd9\u91cc\u7684 onResolved \u51fd\u6570\u8fd4\u56de\u4e86\u4e00\u4e2a 6 \uff0c\u6240\u4ee5\u65b0\u7684 Promise \u7684\u5185\u90e8\u503c\u4f1a\u662f 6\u3002<\/p>\n

\u5982\u679c .then() \u6ca1\u6709\u63d0\u4f9b onResolved \u8fd9\u4e2a\u5904\u7406\u7a0b\u5e8f\uff0c\u5219 Promise.resolve() \u4f1a\u57fa\u4e8e\u4e0a\u4e00\u4e2a\u5b9e\u4f8b resolve \u540e\u7684\u503c\u6765\u521d\u59cb\u5316\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\uff1a<\/p>\n

\r\nlet p1 = new Promise((resolve, reject) => { \r\n    resolve(3) \r\n}) \r\nlet p2 = p1.then() \r\nsetTimeout(console.log, 0, p2)  \/\/ Promise {: 3} \r\n<\/fulfilled><\/pre>\n

\u5982\u679c onResolved \u5904\u7406\u7a0b\u5e8f\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u8fd4\u56de\u7684\u65b0\u5b9e\u4f8b\u7684\u5185\u90e8\u503c\u4f1a\u662f undefined\uff1a<\/p>\n

\r\nlet p1 = new Promise((resolve, reject) => { \r\n    resolve(3) \r\n}) \r\nlet p2 = p1.then(() => {}) \r\nsetTimeout(console.log, 0, p2)  \/\/ Promise {: undefined} \r\n<\/fulfilled><\/pre>\n

\u5982\u679c\u5728 onResolved \u5904\u7406\u7a0b\u5e8f\u91cc\u629b\u51fa\u5f02\u5e38\uff0c\u5219\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 rejected \u72b6\u6001\u7684 Promise\uff1a<\/p>\n

\r\nlet p1 = new Promise((resolve, reject) => { \r\n    resolve(3) \r\n}) \r\nlet p2 = p1.then(() => { \r\n    throw new Error('\u8fd9\u662f\u4e00\u4e2a\u9519\u8bef')} \r\n) \r\nsetTimeout(console.log, 0, p2)  \/\/ Promise {: \u8fd9\u662f\u4e00\u4e2a\u9519\u8bef} \r\n<\/rejected><\/pre>\n
\u57fa\u4e8eonRejected\u751f\u6210\u4e00\u4e2a\u65b0\u7684Promise<\/strong><\/div>\n

\u57fa\u4e8e onRejected \u7684\u8fd4\u56de\u503c\u4e5f\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684 Promise\uff0c\u800c\u4e14\u5904\u7406\u903b\u8f91\u4e5f\u662f\u4e00\u6837\u7684\uff0c\u4e5f\u662f\u901a\u8fc7\u628a\u8fd4\u56de\u503c\u4f20\u9012\u7ed9 Promise.resolve() \u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u5b9e\u4f8b\uff1a<\/p>\n

\r\nlet p1 = new Promise((resolve, reject) => { \r\n    reject(3) \r\n}) \r\n \r\n\/\/ \u6ca1\u6709 `onRejected` \u5904\u7406\u7a0b\u5e8f\u65f6\uff0c\u4f1a\u539f\u6837\u5411\u540e\u4f20\uff0c\u4e0d\u8fc7\u662f\u65b0\u5b9e\u4f8b \r\nlet p2 = p1.then(() => {})  s \r\nsetTimeout(console.log, 0, p2)  \/\/ Promise {: 3} \r\n \r\n\/\/ \u8fd4\u56de\u503c\u4e3aundefined\u65f6 \r\nlet p3 = p1.then(null, () => {})  \r\nsetTimeout(console.log, 0, p3)  \/\/ Promise {: undefined}  \r\n \r\n\/\/ \u8fd4\u56de\u503c\u6709\u5b9e\u9645\u503c\u7684\u65f6\u5019 \r\nlet p4 = p1.then(null, () => 6)  \r\nsetTimeout(console.log, 0, p4)  \/\/ Promise {<\/fulfilled>: 6} \r\n \r\n\/\/ \u5f53\u8fd4\u56de\u503c\u662fPromise\u65f6\uff0c\u4f1a\u4fdd\u7559\u5f53\u524dPromise \r\nlet p5 = p1.then(null, () => Promise.reject())  \r\nsetTimeout(console.log, 0, p5)  \/\/ Promise {: undefined}  \r\n \r\n\/\/ \u5f53\u9047\u5230\u4e00\u4e2a\u9519\u8bef\u7684\u65f6\u5019 \r\nlet p6 = p1.then(null, () => { \r\n    throw new Error('error') \r\n})  \r\nsetTimeout(console.log, 0, p6)  \/\/ Promise {<\/rejected>: error}  \r\n \r\n\/\/ \u5f53\u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u9519\u8bef\u65f6 \r\nlet p7 = p1.then(null, () => new Error('error'))  \r\nsetTimeout(console.log, 0, p7)  \/\/ Promise {: Error: error}  \r\n<\/fulfilled><\/rejected><\/fulfilled><\/rejected><\/pre>\n

\u8fd9\u91cc\u4f60\u4f1a\u4e0d\u4f1a\u6709\u4e2a\u7591\u60d1?\u5b9e\u4f8b resolve() \u7684\u65f6\u5019\uff0c\u72b6\u6001\u7531 pending \u53d8\u6210 rejected\uff0c\u4ece\u800c\u8c03\u7528 onRejected \u8fdb\u884c\u5904\u7406\uff0c\u4f46\u662f\u4e3a\u4ec0\u4e48\u6709\u65f6\u5019\u4f1a\u8fd4\u56de\u4e00\u4e2a fulfilled \u7684\u65b0\u5b9e\u4f8b\u5462?\u8bd5\u7740\u60f3\u4e00\u4e0b\uff0c\u5982\u679c onRejected \u8fd4\u56de\u4e86\u4e00\u4e2a pending \u7684\u6216\u8005 rejected \u72b6\u6001\u7684\u65b0\u5b9e\u4f8b\uff0c\u90a3\u540e\u7eed\u7684\u94fe\u5f0f\u8c03\u7528\u5c31\u8fdb\u884c\u4e0d\u4e0b\u53bb\u4e86\uff0c\u770b\u4e0b\u9762\u4f8b\u5b50\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    reject() \r\n}).then(null, () => { \r\n    console.log('A') \r\n}).then(() => { \r\n    console.log('B') \r\n}).then(() => { \r\n    console.log('C') \r\n}).catch(() => { \r\n    console.log('D') \r\n}) \r\n<\/pre>\n

\u5982\u679c A \u5904\u7406\u51fd\u6570\u8fd9\u91cc\u8fd4\u56de\u4e86\u4e00\u4e2a pending \u72b6\u6001\u7684\u65b0\u5b9e\u4f8b\uff0c\u90a3\u4e48\u540e\u7eed\u6240\u6709\u7684\u94fe\u5f0f\u64cd\u4f5c\u90fd\u65e0\u6cd5\u6267\u884c;\u6216\u8005\u8fd4\u56de\u7684\u662f\u4e00\u4e2a rejected \u72b6\u6001\u7684\u65b0\u5b9e\u4f8b\uff0c\u90a3\u4e48\u540e\u7eed\u7684 B \u548c C \u4e5f\u5c31\u65e0\u6cd5\u6267\u884c\u4e86\uff0c\u90a3\u5c45\u7136\u90fd\u4e0d\u80fd\u6267\u884c B \u548c C \u6240\u5728\u5904\u7406\u7a0b\u5e8f\uff0c\u90a3\u5b9a\u4e49\u6765\u5e72\u561b\u5462?\u94fe\u5f0f\u64cd\u4f5c\u5c31\u6beb\u65e0\u94fe\u5f0f\u53ef\u8a00\u3002\u53c8\uff0conRejected \u7684\u5b58\u5728\u7684\u6839\u672c\u610f\u4e49\u65e0\u975e\u5c31\u662f\u7528\u4e8e\u6355\u83b7 Promise \u4ea7\u751f\u7684\u9519\u8bef\uff0c\u4ece\u800c\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u7684\u6b63\u5e38\u6267\u884c\uff0c\u6240\u4ee5\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u7406\u5e94\u8fd4\u56de\u4e00\u4e2a fulfilled \u7684\u65b0\u5b9e\u4f8b\u3002<\/p>\n

Promise.prototype.catch() \u4e5f\u4f1a\u751f\u6210\u4e00\u4e2a\u65b0\u7684 Promise\uff0c\u5176\u751f\u6210\u89c4\u5219\u548c onRejected \u662f\u4e00\u6837\u7684\u3002<\/p>\n

finally\u751f\u6210\u4e00\u4e2a\u65b0\u7684Promise<\/strong><\/div>\n

\u6ca1\u60f3\u5230\u5427\uff0cPromise.prototype.finally() \u4e5f\u80fd\u751f\u6210\u4e00\u4e2a Promise\u3002finally \u91cc\u7684\u64cd\u4f5c\u662f\u548c\u72b6\u6001\u65e0\u5173\u7684\uff0c\u4e00\u822c\u7528\u6765\u505a\u540e\u7eed\u4ee3\u7801\u7684\u5904\u7406\u5de5\u4f5c\uff0c\u6240\u4ee5 finally \u4e00\u822c\u4f1a\u539f\u6837\u540e\u4f20\u7236 Promise\uff0c\u65e0\u8bba\u7236\u7ea7\u5b9e\u4f8b\u662f\u4ec0\u4e48\u72b6\u6001\u3002<\/p>\n

\r\nlet p1 = new Promise(() => {}) \r\nlet p2 = p1.finally(() => {}) \r\nsetTimeout(console.log, 0, p2)  \/\/ Promise {} \r\n \r\nlet p3 = new Promise((resolve, reject) => { \r\n    resolve(3) \r\n}) \r\nlet p4 = p3.finally(() => {}) \r\nsetTimeout(console.log, 0, p3)  \/\/ Promise {: 3} \r\n<\/fulfilled><\/pending><\/pre>\n

\u4e0a\u9762\u8bf4\u7684\u662f\u4e00\u822c\uff0c\u4f46\u662f\u4e5f\u6709\u7279\u6b8a\u60c5\u51b5\uff0c\u6bd4\u5982 finally \u91cc\u8fd4\u56de\u4e86\u4e00\u4e2a\u975e fulfilled \u7684 Promise \u6216\u8005\u629b\u51fa\u4e86\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u5219\u4f1a\u8fd4\u56de\u5bf9\u5e94\u72b6\u6001\u7684\u65b0\u5b9e\u4f8b\uff1a<\/p>\n

\r\nlet p1 = new Promise((resolve, reject) => { \r\n    resolve(3) \r\n}) \r\nlet p2 = p1.finally(() => new Promise(() => {})) \r\nsetTimeout(console.log, 0, p2)  \/\/ Promise {} \r\n \r\nlet p3 = p1.finally(() => Promise.reject(6)) \r\nsetTimeout(console.log, 0, p3)  \/\/ Promise {: 6} \r\n \r\nlet p4 = p1.finally(() => { \r\n    throw new Error('error') \r\n}) \r\nsetTimeout(console.log, 0, p4)  \/\/ Promise {<\/rejected>: Error: error} \r\n<\/rejected><\/pending><\/pre>\n
\u6267\u884c\u987a\u5e8f<\/strong><\/div>\n

\u5148\u6765\u770b\u4e00\u6bb5\u7b80\u5355\u7684\u4ee3\u7801\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    console.log('A') \r\n    resolve(3) \r\n    console.log('B') \r\n}).then(res => { \r\n    console.log('C') \r\n}) \r\nconsole.log('D') \r\n\/\/ \u6253\u5370\u7ed3\u679c\uff1aA B D C \r\n<\/pre>\n

\u4e0a\u9762\u8fd9\u4e32\u4ee3\u7801\u7684\u8f93\u51fa\u987a\u5e8f\u662f\uff1aA B D C\u3002\u4ece\u4e0a\u9762\u7ae0\u8282\u4ecb\u7ecd\u7684\u77e5\u8bc6\u70b9\u6211\u4eec\u77e5\u9053\uff0cexecutor \u6267\u884c\u5668\u4f1a\u5728 new Promise \u8c03\u7528\u7684\u65f6\u5019\u7acb\u5373\u540c\u6b65\u6267\u884c\u7684\uff0c\u6240\u4ee5\u5148\u540e\u6253\u5370 A B \u662f\u6ca1\u95ee\u9898\u7684\u3002\u5f53\u6267\u884c resolve()\/reject() \u7684\u65f6\u5019\uff0c\u4f1a\u5c06 Promise \u5bf9\u5e94\u7684\u5904\u7406\u7a0b\u5e8f\u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u7a0d\u7b49\u8fd9\u91cc\u63d0\u5230\u7684\u5bf9\u5e94\u7684\u5904\u7406\u7a0b\u5e8f\u5177\u4f53\u662f\u6307\u4ec0\u4e48?<\/p>\n

resolve() \u5bf9\u5e94 .then() \u91cc\u7684\u7b2c\u4e00\u4e2a\u5165\u53c2\uff0c\u5373 onResolved \u51fd\u6570;
\nreject() \u5bf9\u5e94 .then() \u91cc\u7684\u7b2c\u4e8c\u4e2a\u5165\u53c2\uff0c\u5373 onRejected \u51fd\u6570;\u6216\u8005 Promise.prototype.catch() \u91cc\u7684\u56de\u8c03\u51fd\u6570;
\n\u6240\u4ee5\u5f53\u6267\u884c resolve(3) \u7684\u65f6\u5019(\u6b64\u65f6\u4e0b\u9762\u5b9a\u4e49\u7684\u8fd9\u4e2a\u7bad\u5934\u51fd\u6570\u5176\u5b9e\u5c31\u662f onResolved \u51fd\u6570)\uff0conResolved \u51fd\u6570\u5c06\u88ab\u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u7136\u540e\u6253\u5370 D\uff0c\u6b64\u65f6\u6240\u6709\u540c\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210\uff0c\u6d4f\u89c8\u5668\u4f1a\u53bb\u68c0\u67e5\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u53d1\u73b0\u5b58\u5728\u4e00\u4e2a\uff0c\u6240\u4ee5\u6700\u540e\u4f1a\u53bb\u8c03\u7528 onResolved \u51fd\u6570\uff0c\u6253\u5370\u51fa C\u3002<\/p>\n

\r\nlet onResolved = res => { \r\n    console.log('C') \r\n} \r\n<\/pre>\n

\u5176\u5b9e\u9664\u4e86 onResolved\u3001onRejected \u4ee5\u53ca Promise.prototype.catch() \u91cc\u7684\u5904\u7406\u7a0b\u5e8f\u5916\uff0cPromise.prototype.finally() \u7684\u5904\u7406\u7a0b\u5e8f onFinally \u4e5f\u662f\u5f02\u6b65\u6267\u884c\u7684\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    console.log('A') \r\n    resolve(3) \r\n}).finally(() => { \r\n    console.log('B') \r\n}) \r\nconsole.log('C') \r\n\/\/ \u6253\u5370\u7ed3\u679c\uff1aA C B \r\n<\/pre>\n

Promise \u94fe\u5f0f\u8c03\u7528\u7684\u57fa\u7840\u5c31\u662f\u56e0\u4e3a onResolved\u3001onRejected\u3001catch() \u7684\u5904\u7406\u7a0b\u5e8f\u4ee5\u53ca onFinally \u4f1a\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684 Promise \u5b9e\u4f8b\uff0c\u4e14\u53c8\u56e0\u4e3a\u4ed6\u4eec\u90fd\u662f\u5f02\u6b65\u6267\u884c\u7684\uff0c\u6240\u4ee5\u5728\u94fe\u5f0f\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5bf9\u4e8e\u5b83\u4eec\u6267\u884c\u987a\u5e8f\u4f1a\u7a00\u91cc\u7cca\u6d82\u7422\u78e8\u4e0d\u900f\u5c31\u662f\u8fd9\u4e2a\u539f\u56e0\u3002<\/p>\n

\u9898\u76ee\u4e00<\/strong><\/div>\n

\u90a3\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u770b\u70b9\u590d\u6742\u7684\u4f8b\u5b50\uff0c\u5148\u6765\u5206\u6790\u4e0b\u8fd9\u7ae0\u5f00\u7bc7\u63d0\u5230\u7684\u9898\u76ee\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    resolve() \r\n}).then(() => { \r\n    console.log('A') \r\n    new Promise((resolve, reject) => { \r\n        resolve() \r\n    }).then(() => { \r\n        console.log('B') \r\n    }).then(() => { \r\n        console.log('C') \r\n    }) \r\n}).then(() => { \r\n    console.log('D') \r\n}) \r\n\/\/ \u6253\u5370\u7ed3\u679c\uff1aA \r\n<\/pre>\n

\u4e3a\u4e86\u65b9\u4fbf\u5206\u6790\uff0c\u6211\u4eec\u628a\u4e0a\u9762\u7684\u8fd9\u4e32\u4ee3\u7801\u5199\u5f97\u597d\u770b\u4e00\u70b9\uff1a<\/p>\n

\r\nnew Promise(executor).then(onResolvedA).then(onResolvedD) \r\n \r\nfunction executor(resolve, reject) { \r\n    resolve() \r\n} \r\nfunction onResolvedA() { \r\n    console.log('A') \r\n    new Promise(executor).then(onResolvedB).then(onResolvedC) \r\n} \r\nfunction onResolvedB() { \r\n    console.log('B') \r\n} \r\nfunction onResolvedC() { \r\n    console.log('C') \r\n} \r\nfunction onResolvedD() { \r\n    console.log('D') \r\n} \r\n<\/pre>\n

\u6267\u884c\u8fc7\u7a0b\uff1a<\/p>\n

\u6267\u884c new Promise()\uff0c\u7acb\u5373\u540c\u6b65\u6267\u884c executor \u51fd\u6570\uff0c\u8c03\u7528 resolve()\uff0c\u6b64\u65f6\u4f1a\u5c06 onResolvedA \u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 1\uff0c\u622a\u6b62\u76ee\u524d\u6240\u6709\u540c\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6210;
\n\u68c0\u67e5\u5fae\u4efb\u52a1\u961f\u5217\uff0c\u6267\u884c onResolvedA \u51fd\u6570\uff0c\u6253\u5370 A\uff0c\u6267\u884c new Promise(executor)\uff0c\u8c03\u7528 resolve() \u51fd\u6570\uff0c\u6b64\u65f6\u5c06 onResolvedB \u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 2;
\n\u622a\u6b62\u76ee\u524d\u5fae\u4efb\u52a1\u961f\u5217 1 \u7684\u4ee3\u7801\u5168\u90e8\u6267\u884c\u5b8c\u6210\uff0c\u5373 onResolvedA \u51fd\u6570\u6267\u884c\u5b8c\u6210\u3002\u6211\u4eec\u77e5\u9053 onResolved \u51fd\u6570\u4f1a\u57fa\u4e8e\u8fd4\u56de\u503c\u751f\u6210\u4e00\u4e2a\u65b0\u7684 Promise\uff0c\u800c onResolvedA \u51fd\u6570\u6ca1\u6709\u663e\u793a\u7684\u8fd4\u56de\u503c\uff0c\u6240\u4ee5\u5176\u8fd4\u56de\u503c\u4e3a undefined\uff0c\u90a3\u4e48\u7ecf\u8fc7 Promise.resolve(undefined) \u521d\u59cb\u5316\u540e\u4f1a\u751f\u6210\u4e00\u4e2a\u8fd9\u6837\u7684\u65b0\u5b9e\u4f8b\uff1aPromise {: undefined};\u7531\u4e8e\u8fd9\u4e2a\u65b0\u7684\u5b9e\u4f8b\u72b6\u6001\u5df2\u7ecf\u53d8\u6210 fulfilled\uff0c\u6240\u4ee5\u4f1a\u7acb\u5373\u5c06\u5176\u5904\u7406\u51fd\u6570 onResolvedD \u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 3;
\n\u5f00\u59cb\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 2 \u91cc\u7684\u5185\u5bb9\uff0c\u6253\u5370 B\uff0c\u540c\u4e0a\u4e00\u6761\u539f\u7406\uff0c\u7531\u4e8e onResolvedB \u51fd\u6570\u7684\u8fd4\u56de\u503c\u4e3a undefined\uff0c\u6240\u4ee5\u751f\u6210\u4e86\u4e00\u4e2a resolved \u7684\u65b0\u5b9e\u4f8b\uff0c\u5219\u4f1a\u7acb\u5373\u5c06 onResolvedC \u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 4;
\n\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 3\uff0c\u6253\u5370 D;
\n\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 4\uff0c\u6253\u5370 C;
\n\u81f3\u6b64\u5168\u90e8\u4ee3\u7801\u6267\u884c\u5b8c\u6210\uff0c\u6700\u7ec8\u7684\u6253\u5370\u7ed3\u679c\u4e3a\uff1aA B D C\u3002<\/p>\n

\u9898\u76ee\u4e8c<\/strong><\/div>\n
\r\nnew Promise((resolve, reject) => { \r\n    resolve(1) \r\n}).then(res => { \r\n    console.log('A') \r\n}).finally(() => { \r\n    console.log('B') \r\n}) \r\nnew Promise((resolve, reject) => { \r\n    resolve(2) \r\n}).then(res => { \r\n    console.log('C') \r\n}).finally(() => { \r\n    console.log('D') \r\n}) \r\n\/\/ \u6253\u5370\u7ed3\u679c\uff1aA C B D \r\n<\/pre>\n

\u5e94\u8be5\u5f88\u591a\u4eba\u4f1a\u548c\u6211\u5f53\u521d\u4e00\u6837\u597d\u5947\uff1a\u4e3a\u4ec0\u4e48\u6253\u5370\u7ed3\u679c\u4e0d\u662f A B C D \u5462?\u8fd9\u91cc\u6d89\u53ca\u5230\u4e00\u4e2a\u77e5\u8bc6\u70b9\uff1a\u5982\u679c\u7ed9 Promise \u5b9e\u4f8b\u6dfb\u52a0\u4e86\u591a\u4e2a\u5904\u7406\u51fd\u6570\uff0c\u5f53\u5b9e\u4f8b\u72b6\u6001\u53d8\u5316\u7684\u65f6\u5019\uff0c\u90a3\u4e48\u6267\u884c\u7684\u8fc7\u7a0b\u5c31\u662f\u6309\u7167\u6dfb\u52a0\u65f6\u7684\u987a\u5e8f\u800c\u6267\u884c\u7684\u3002<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    resolve(1) \r\n}).then(onResolvedA).finally(onFinally) \r\n \r\nfunction onResolvedA() { \r\n    console.log('A') \r\n} \r\nfunction onFinally() { \r\n    console.log('B') \r\n} \r\n\/\/ \u6253\u5370\u7ed3\u679c\uff1aA B \r\n<\/pre>\n

\u5bf9\u4e8e\u4e0a\u9762\u8fd9\u4e32\u4ee3\u7801\uff0c\u5176\u5b9e finally() \u5904\u7406\u7a0b\u5e8f\u6267\u884c\u7684\u65f6\u5019\u5df2\u7ecf\u4e0d\u662f\u901a\u8fc7 new Promise() \u521d\u59cb\u5316\u7684\u5b9e\u4f8b\uff0c\u800c\u662f\u6267\u884c\u5b8c onResolvedA \u51fd\u6570\u7684\u65f6\u5019\u751f\u6210\u7684\u65b0\u5b9e\u4f8b\uff0c\u4e0d\u4fe1\u6211\u4eec\u5c06\u4e0a\u9762\u4ee3\u7801\u4e2d\u7684\u51fd\u6570 onResolvedA \u7a0d\u5fae\u6539\u52a8\u4e0b\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    resolve(1) \r\n}).then(onResolvedA).finally(onFinally) \r\n \r\nfunction onResolvedA() { \r\n    console.log('A') \r\n    return new Promise(() => {}) \r\n} \r\nfunction onFinally() { \r\n    console.log('B') \r\n} \r\n\/\/ \u6253\u5370\u7ed3\u679c\uff1aA \r\n<\/pre>\n

\u7531\u4e8e onResolvedA \u8fd4\u56de\u4e86\u4e00\u4e2a\u8fd9\u6837\u7684 Promise { } \u65b0\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u65b0\u5b9e\u4f8b\u7684\u72b6\u6001\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0c\u6240\u4ee5\u4e0d\u4f1a\u6267\u884c finally \u5904\u7406\u7a0b\u5e8f onFinally\uff0c\u6240\u4ee5\u4e0d\u4f1a\u6253\u5370 B\u3002\u8fd9\u4e2a\u5c31\u8bf4\u660e\u4e86\uff0c\u94fe\u5f0f\u8c03\u7528\u7684\u65f6\u5019\u5904\u7406\u7a0b\u5e8f\u7684\u6267\u884c\u662f\u4e00\u6b65\u4e00\u6b65\u6765\u7684\uff0c\u53ea\u8981\u524d\u9762\u7684\u6267\u884c\u5b8c\u4e86\uff0c\u751f\u6210\u4e86\u65b0\u7684\u5b9e\u4f8b\uff0c\u7136\u540e\u6839\u636e\u65b0\u5b9e\u4f8b\u7684\u72b6\u6001\u53d8\u5316\uff0c\u624d\u53bb\u6267\u884c\u540e\u7eed\u7684\u5904\u7406\u7a0b\u5e8f\u3002<\/p>\n

\u6240\u4ee5\u62ff\u6700\u5f00\u59cb\u90a3\u9053\u9898\u6765\u8bf4\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    resolve(1) \r\n}).then(res => { \r\n    console.log('A') \r\n}).finally(() => { \r\n    console.log('B') \r\n}) \r\nnew Promise((resolve, reject) => { \r\n    resolve(2) \r\n}).then(res => { \r\n    console.log('C') \r\n}).finally(() => { \r\n    console.log('D') \r\n}) \r\n\/\/ \u6253\u5370\u7ed3\u679c\uff1aA C B D \r\n<\/pre>\n

\u4ed6\u7684\u6267\u884c\u8fc7\u7a0b\u5e94\u8be5\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n

\u6267\u884c resolve(1)\uff0c\u5c06\u5904\u7406\u7a0b\u5e8f A \u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 1;
\n\u6267\u884c resolve(2)\uff0c\u5c06\u5904\u7406\u7a0b\u5e8f C \u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 2;
\n\u540c\u6b65\u4efb\u52a1\u6267\u884c\u5b8c\u6210\uff0c\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 1 \u91cc\u7684\u5185\u5bb9\uff0c\u6253\u5370 A\uff0cA \u6240\u5728\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\u751f\u6210\u4e86\u4e00\u4e2a fulfilled \u7684\u65b0\u5b9e\u4f8b\uff0c\u7531\u4e8e\u65b0\u5b9e\u4f8b\u72b6\u6001\u53d8\u5316\uff0c\u6240\u4ee5\u4f1a\u7acb\u5373\u6267\u884c finally() \u5904\u7406\u7a0b\u5e8f B \u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 3;
\n\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 2 \u7684\u5185\u5bb9\uff0c\u6253\u5370 C\uff0cC \u6240\u5728\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\uff0c\u540c\u4e0a\u6761\u539f\u7406\u4f1a\u5c06\u5904\u7406\u7a0b\u5e8f D \u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 4;
\n\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 3 \u7684\u5185\u5bb9\uff0c\u6253\u5370 B;
\n\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 4 \u7684\u5185\u5bb9\uff0c\u6253\u5370 D;
\n\u4ee3\u7801\u5168\u90e8\u6267\u884c\u5b8c\u6210\uff0c\u6700\u7ec8\u6253\u5370\uff1aA C B D\u3002
\n\u9898\u76ee\u5c31\u5148\u505a\u5230\u8fd9\u91cc\uff0c\u76f8\u4fe1\u4f60\u548c\u6211\u4e00\u6837\uff0c\u5bf9 Promise \u7684\u6267\u884c\u8fc7\u7a0b\u5e94\u8be5\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\u4e86\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u7ee7\u7eed\u5b66\u4e60 Promise \u7684\u76f8\u5173 API\u3002<\/p>\n

Promise\u4e0e\u9519\u8bef\u5904\u7406<\/strong><\/div>\n

\u5e73\u65f6\u6211\u4eec\u5199\u4ee3\u7801\u9047\u5230\u9519\u8bef\uff0c\u90fd\u4e60\u60ef\u7528 try\/catch \u5757\u6765\u5904\u7406\uff0c\u4f46\u662f\u5bf9\u4e8e Promise \u4ea7\u751f\u7684\u9519\u8bef\uff0c\u7528\u8fd9\u4e2a\u662f\u5904\u7406\u4e0d\u4e86\u7684\uff0c\u770b\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n

\r\ntry { \r\n    new Promise((resolve, reject) => { \r\n        console.log('A') \r\n        throw new Error() \r\n        console.log('B') \r\n    })   \r\n} catch(err) { \r\n    console.log(err) \r\n} \r\nconsole.log('C') \r\n\/\/ A \r\n\/\/ C  \r\n\/\/ Uncaught (in promise) Error \r\n<\/pre>\n

\u4ece\u6267\u884c\u7ed3\u679c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u62a5\u9519\u7684\u4fe1\u606f\u51fa\u73b0\u5728\u6253\u5370 C \u4e4b\u540e\uff0c\u8bf4\u660e\u629b\u51fa\u9519\u8bef\u8fd9\u4e2a\u52a8\u4f5c\u662f\u5728\u5f02\u6b65\u4efb\u52a1\u4e2d\u505a\u7684\uff0c\u6240\u4ee5 catch \u6355\u83b7\u4e0d\u5230\u8be5\u9519\u8bef\u5c31\u5728\u60c5\u7406\u4e4b\u4e2d\u4e86\uff0c\u5426\u5219\u5c31\u4e0d\u4f1a\u6253\u5370 C \u4e86\u3002\u53ef\u89c1\uff0c\u4f20\u7edf\u7684 try\/catch \u8bed\u53e5\u5e76\u4e0d\u80fd\u6355\u83b7 Promise \u4ea7\u751f\u7684\u9519\u8bef\uff0c\u800c\u9700\u8981\u4f7f\u7528 onRejected \u5904\u7406\u7a0b\u5e8f\uff1a<\/p>\n

\r\nlet p1 = new Promise((resolve, reject) => { \r\n    console.log('A') \r\n    throw new Error('error') \r\n    console.log('B') \r\n}) \r\nlet p2 = p1.catch((err) => { \r\n    console.log(err) \r\n})  \r\nsetTimeout(console.log, 0, p2) \r\n\/\/ A \r\n\/\/ Error: error \r\n\/\/ Promise {: undefined} \r\n<\/fulfilled><\/pre>\n

onRejected \u6355\u83b7\u4e86\u4e0a\u9762\u629b\u51fa\u7684\u9519\u8bef\u540e\uff0c\u4f7f\u5f97\u7a0b\u5e8f\u6b63\u5e38\u6267\u884c\uff0c\u6700\u540e\u8fd8\u751f\u6210\u4e86\u4e00\u4e2a fulfilled\u7684\u65b0\u5b9e\u4f8b\u3002<\/p>\n

\u9664\u4e86\u4ee5\u4e0a\u8fd9\u79cd\u76f4\u63a5\u5728 executor \u91cc\u901a\u8fc7 throw \u4e3b\u52a8\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u4ea7\u51fa\u9700\u8981 onRejected \u5904\u7406\u7684\u9519\u8bef\uff1a<\/p>\n

\r\nnew Promise((resolve, reject) => { \r\n    init() \/\/ \u88ab\u52a8\u51fa\u9519\uff0c\u8c03\u7528\u4e86\u4e0d\u5b58\u5728\u7684\u51fd\u6570 \r\n}) \r\n \r\nnew Promise((resolve, reject) => { \r\n    reject() \r\n}) \r\n \r\nnew Promise((resolve, reject) => { \r\n    resolve() \r\n}).then(() => Promise.reject()) \r\n \r\nnew Promise((resolve, reject) => { \r\n    resolve() \r\n}).then(() => { \r\n    throw new Error() \r\n}) \r\n<\/pre>\n

\u6ce8\u610f\uff0c\u5982\u679c\u53ea\u662f\u4ea7\u751f\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u5374\u6ca1\u6709\u629b\u51fa\u6765\u662f\u4e0d\u4f1a\u62a5\u9519\u7684\uff1a<\/p>\n

\r\n\/\/ \u4e0d\u4f1a\u62a5\u9519 \r\nnew Promise((resolve, reject) => { \r\n    reject() \r\n}).then(() => new Error()) \r\n<\/pre>\n

Promise \u51fa\u73b0\u4e86\u9519\u8bef\u5c31\u9700\u8981\u4f7f\u7528 onRejected \u5904\u7406\u7a0b\u5e8f\u5904\u7406\uff0c\u5426\u5219\u7a0b\u5e8f\u5c31\u4f1a\u62a5\u9519\uff0c\u6267\u884c\u4e0d\u4e0b\u53bb\u4e86\u3002<\/p>\n

Promise API<\/strong><\/div>\n
Promise.resolve()<\/strong><\/span><\/div>\n

\u5e76\u975e\u6240\u6709\u7684 Promise \u7684\u521d\u59cb\u72b6\u6001\u90fd\u662f pending\uff0c\u53ef\u4ee5\u901a\u8fc7 Promise.resolve(value) \u6765\u521d\u59cb\u5316\u4e00\u4e2a\u72b6\u6001\u4e3a fulfilled\uff0c\u503c\u4e3a value \u7684 Promise \u5b9e\u4f8b\uff1a<\/p>\n

\r\nlet p = Promise.resolve(3) \r\nconsole.log(p)  \/\/ Promise {: 3} \r\n<\/fulfilled><\/pre>\n

\u8fd9\u4e2a\u64cd\u4f5c\u548c\u4e0b\u9762\u8fd9\u79cd\u521b\u5efa\u4e00\u4e2a fulfilled \u7684 Promise \u5728\u6548\u679c\u4e0a\u662f\u4e00\u6837\u7684\uff1a<\/p>\n

\r\nlet p = new Promise(resolve => resolve(3)) \r\nconsole.log(p)  \/\/ Promise {: 3} \r\n<\/fulfilled><\/pre>\n

\u4f7f\u7528\u8fd9\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u7406\u8bba\u4e0a\u53ef\u4ee5\u628a\u4efb\u4f55\u4e00\u4e2a\u503c\u8f6c\u6362\u6210 Promise\uff1a<\/p>\n

\r\nsetTimeout(console.log, 0, Promise.resolve())  \/\/ Promise {: undefined} \r\nsetTimeout(console.log, 0, Promise.resolve(3, 6, 9))  \/\/ Promise {<\/fulfilled>: 3} \u591a\u4f59\u7684\u53c2\u6570\u5c06\u88ab\u5ffd\u7565 \r\nsetTimeout(console.log, 0, Promise.resolve(new Error('error')))  \/\/ Promise {<\/fulfilled>: Error: error} \r\n<\/fulfilled><\/pre>\n

\u8fd9\u4e2a\u88ab\u8f6c\u6362\u7684\u503c\u751a\u81f3\u53ef\u4ee5\u662f\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u5982\u679c\u662f\u8fd9\u6837\uff0cPromise.resolve \u4f1a\u5c06\u5176\u539f\u6837\u8f93\u51fa\uff1a<\/p>\n

\r\nlet p = Promise.resolve(3) \r\nsetTimeout(console.log, 0, p === Promise.resolve(p))  \/\/ true \r\n<\/pre>\n
Promise.reject()<\/strong><\/span><\/div>\n

\u548c Promise.resolve() \u7c7b\u4f3c\uff0cPromise.reject() \u4f1a\u5b9e\u4f8b\u5316\u4e00\u4e2a rejected \u72b6\u6001\u7684 Promise\uff0c\u4e14\u4f1a\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\uff0c\u8be5\u9519\u8bef\u53ea\u80fd\u901a\u8fc7\u62d2\u7edd\u5904\u7406\u7a0b\u5e8f\u6355\u83b7\u3002<\/p>\n

\r\nPromise \r\n    .reject(3) \r\n    .catch(err => { \r\n        console.log(err)  \/\/ 3 \r\n    }) \r\n<\/pre>\n

\u5bf9\u4e8e\u521d\u59cb\u5316\u4e00\u4e2a rejected \u72b6\u6001\u7684\u5b9e\u4f8b\uff0c\u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\u90fd\u53ef\u4ee5\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\uff1a<\/p>\n

\r\nlet p1 = Promise.reject() \r\nlet p2 = new Promise((resolve, reject) => reject())\r\n<\/pre>\n

\u4e0e Promise.resolve() \u4e0d\u540c\u7684\u662f\uff0c\u5982\u679c\u7ed9 Promise.reject() \u4f20\u9012\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u5219\u8fd9\u4e2a\u5bf9\u8c61\u4f1a\u6210\u4e3a\u65b0 Promise \u7684\u503c\uff1a<\/p>\n

\r\nlet p = Promise.reject(3) \r\nsetTimeout(console.log, 0, p === Promise.reject(p))  \/\/ false \r\n<\/pre>\n
Promise.all()<\/strong><\/span><\/div>\n

Promise.all(iterable) \u7528\u6765\u5c06\u591a\u4e2a Promise \u5b9e\u4f8b\u5408\u6210\u4e00\u4e2a\u65b0\u5b9e\u4f8b\u3002\u53c2\u6570\u5fc5\u987b\u662f\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u901a\u5e38\u662f\u6570\u7ec4\u3002<\/p>\n

\r\nPromise.all([ \r\n    Promise.resolve(3), \r\n    Promise.resolve(6) \r\n]) \r\n<\/pre>\n

\u53ef\u8fed\u4ee3\u5bf9\u8c61\u91cc\u7684\u6240\u6709\u5143\u7d20\u90fd\u4f1a\u901a\u8fc7 Promise.resolve() \u8f6c\u6210 Promise\uff1a<\/p>\n

\r\nPromise.all([3, 6, 9]) \r\n<\/pre>\n

\u6240\u6709 Promise \u90fd resolve \u540e\uff0cPromise.all() \u624d\u4f1a\u751f\u6210\u4e00\u4e2a fulfilled \u7684\u65b0\u5b9e\u4f8b\u3002\u4e14\u65b0\u5b9e\u4f8b\u7684\u5185\u90e8\u503c\u662f\u7531\u6240\u6709 Promise \u89e3\u51b3\u540e\u7684\u503c\u7ec4\u6210\u7684\u6570\u7ec4\uff1a<\/p>\n

\r\nlet p1 = Promise.all([ \r\n    Promise.resolve('3'), \r\n    Promise.resolve(), \r\n    6 \r\n]) \r\nlet p2 = p1.then(res => { \r\n    console.log(res) \r\n}) \r\nsetTimeout(console.log, 0, p1) \r\n\/\/ [\"3\", undefined, 6] \r\n\/\/ Promise {: Array(3)} \r\n<\/fulfilled><\/pre>\n

\u6240\u6709 Promise \u4e2d\uff0c\u53ea\u8981\u51fa\u73b0\u4e00\u4e2a pending \u72b6\u6001\u7684\u5b9e\u4f8b\uff0c\u90a3\u4e48\u5408\u6210\u7684\u65b0\u5b9e\u4f8b\u4e5f\u662f pending \u72b6\u6001\u7684\uff1a<\/p>\n

\r\nlet p1 = Promise.all([ \r\n    3, \r\n    Promise.resolve(6), \r\n    new Promise(() => {}) \r\n]) \r\nsetTimeout(console.log, 0, p1) \r\n\/\/ Promise {} \r\n<\/pending><\/pre>\n

\u6240\u6709 Promise \u4e2d\uff0c\u53ea\u8981\u51fa\u73b0\u4e00\u4e2a rejected \u72b6\u6001\u7684\u5b9e\u4f8b\uff0c\u90a3\u4e48\u5408\u6210\u7684\u65b0\u5b9e\u4f8b\u4e5f\u662f rejected\u72b6\u6001\u7684\uff0c\u4e14\u65b0\u5b9e\u4f8b\u7684\u5185\u90e8\u503c\u662f\u7b2c\u4e00\u4e2a\u62d2\u7edd Promise \u7684\u5185\u90e8\u503c\uff1a<\/p>\n

\r\nlet p1 = Promise.all([ \r\n    3, \r\n    Promise.reject(6), \r\n    new Promise((resolve, reject) => { \r\n        reject(9) \r\n    }) \r\n]) \r\nlet p2 = p1.catch(err => { \r\n    console.log(err) \r\n}) \r\nsetTimeout(console.log, 0, p1) \r\n\/\/ 6 \r\n\/\/ Promise {: 6} \r\n<\/rejected><\/pre>\n
Promise.race()<\/strong><\/span><\/div>\n

Promise.race(iterable) \u4f1a\u8fd4\u56de\u4e00\u4e2a\u7531\u6240\u6709\u53ef\u8fed\u4ee3\u5b9e\u4f8b\u4e2d\u7b2c\u4e00\u4e2a fulfilled \u6216 rejected\u7684\u5b9e\u4f8b\u5305\u88c5\u540e\u7684\u65b0\u5b9e\u4f8b\u3002<\/p>\n

\r\nlet p1 = Promise.race([ \r\n    3, \r\n    Promise.reject(6), \r\n    new Promise((resolve, reject) => { \r\n        resolve(9) \r\n    }).then(res => { \r\n        console.log(res) \r\n    }) \r\n]) \r\nlet p2 = p1.then(res => { \r\n    console.log(err) \r\n}) \r\nsetTimeout(console.log, 0, p1) \r\n\/\/ 9 \r\n\/\/ 3 \r\n\/\/ Promise {: 3} \r\n<\/fulfilled><\/pre>\n

\u6765\u5c06\u4e0a\u9762\u8fd9\u4e32\u4ee3\u7801\u53d8\u52a8\u4e0b\uff1a<\/p>\n

\r\nfunction init(){ \r\n    console.log(3) \r\n    return 3 \r\n} \r\nlet p1 = Promise.race([ \r\n    new Promise((resolve, reject) => { \r\n        resolve(9) \r\n    }).then(res => { \r\n        console.log(res) \r\n        return 'A' \r\n    }), \r\n    new Promise((resolve, reject) => { \r\n        reject(6) \r\n    }), \r\n    init(), \r\n]) \r\nlet p2 = p1.then(res => { \r\n    console.log(res) \r\n}, err => { \r\n    console.log(err) \r\n}) \r\nsetTimeout(console.log, 0, p1) \r\n\/\/ 3 \r\n\/\/ 9 \r\n\/\/ 6 \r\n\/\/ Promise {: 6} \r\n<\/rejected><\/pre>\n

\u60f3\u8981\u77e5\u9053 Promise.race() \u7684\u7ed3\u679c\uff0c\u65e0\u975e\u662f\u8981\u77e5\u9053\u5230\u5e95\u8c01\u624d\u662f\u7b2c\u4e00\u4e2a\u72b6\u6001\u53d8\u5316\u7684\u5b9e\u4f8b\uff0c\u8ba9\u6211\u4eec\u6765\u5177\u4f53\u5206\u6790\u4e0b\u4ee3\u7801\u6267\u884c\u8fc7\u7a0b\uff1a<\/p>\n

\u8fed\u4ee3\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u6267\u884c\u540c\u6b65\u4ee3\u7801 resolve(9)\uff0c\u7531 new Promise \u521d\u59cb\u5316\u7684\u5b9e\u4f8b\u7684\u72b6\u6001\u5df2\u7ecf\u53d8\u4e3a\u4e86 fulfilled\uff0c\u6240\u4ee5\u7b2c\u4e00\u4e2a\u72b6\u6001\u53d8\u5316\u7684\u5b9e\u4f8b\u5df2\u7ecf\u51fa\u73b0\u4e86\u5417?\u5176\u5b9e\u5e76\u6ca1\u6709\uff0c\u56e0\u4e3a\u8fed\u4ee3\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u4ee3\u7801\u8fd8\u6ca1\u6267\u884c\u5b8c\u6210\u5462\uff0c\u7136\u540e\u4f1a\u5c06 return 'A' \u6240\u5728\u51fd\u6570\u7684\u8fd9\u6bb5\u5904\u7406\u7a0b\u5e8f\u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 1;
\n\u8fed\u4ee3\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u6267\u884c reject(6)\uff0c\u6240\u4ee5\u7531 new Promise \u521d\u59cb\u5316\u7684\u5b9e\u4f8b\u7684\u72b6\u6001\u5df2\u7ecf\u53d8\u4e3a rejected\uff0c\u7531\u4e8e\u8be5\u5b9e\u4f8b\u6ca1\u6709\u5904\u7406\u51fd\u6570\uff0c\u6240\u4ee5\u8fed\u4ee3\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u4ee3\u7801\u5df2\u7ecf\u5168\u90e8\u6267\u884c\u5b8c\u6210\uff0c\u6b64\u65f6\uff0c\u7b2c\u4e00\u4e2a\u72b6\u6001\u53d8\u5316\u7684\u5b9e\u4f8b\u5df2\u7ecf\u4ea7\u751f;
\n\u8fed\u4ee3\u7b2c\u4e09\u4e2a\u5143\u7d20\uff0c\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u6267\u884c\u540c\u6b65\u4ee3\u7801\u6253\u5370\u51fa 3\uff0c\u7136\u540e\u7528 Promise.resolve \u5c06\u51fd\u6570\u8fd4\u56de\u503c 3 \u8f6c\u6210\u4e00\u4e2a Promise {: 3} \u7684\u65b0\u5b9e\u4f8b\uff0c\u8fd9\u662f\u7b2c\u4e8c\u4e2a\u72b6\u6001\u53d1\u751f\u53d8\u5316\u7684\u5b9e\u4f8b;
\n\u6b64\u65f6\u6240\u6709\u8fed\u4ee3\u5bf9\u8c61\u904d\u5386\u5b8c\u6210\uff0c\u5373\u540c\u6b65\u4ee3\u7801\u6267\u884c\u5b8c\u6210\uff0c\u5f00\u59cb\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 1 \u7684\u5185\u5bb9\uff0c\u6253\u5370 res\uff0c\u5176\u503c\u662f 9\uff0c\u7136\u540e\u5904\u7406\u7a0b\u5e8f\u8fd4\u56de\u4e86 'A'\uff0c\u6b64\u65f6\u6839\u636e\u4e4b\u524d\u63d0\u5230\u7684\u77e5\u8bc6\u70b9\uff0c\u8fd9\u91cc\u4f1a\u65b0\u751f\u6210\u4e00\u4e2a Promise {: 'A'} \u7684\u5b9e\u4f8b\uff0c\u8fd9\u662f\u7b2c\u4e09\u4e2a\u72b6\u6001\u53d1\u751f\u53d8\u5316\u7684\u5b9e\u4f8b\u3002\u6b64\u65f6\uff0c\u7b2c\u4e00\u4e2a\u8fed\u4ee3\u5143\u7d20\u7684\u4ee3\u7801\u5df2\u7ecf\u5168\u90e8\u6267\u884c\u5b8c\u6210\uff0c\u6240\u4ee5\u7b2c\u4e00\u4e2a\u8fed\u4ee3\u5143\u7d20\u6700\u7ec8\u751f\u6210\u7684\u5b9e\u4f8b\u662f\u7b2c\u4e09\u6b21\u72b6\u6001\u53d1\u751f\u53d8\u5316\u7684\u8fd9\u4e2a;
\n\u6b64\u65f6 p1 \u5df2\u7ecf\u4ea7\u751f\uff0c\u5b83\u662f Promise {: 6}\uff0c\u6240\u4ee5\u4f1a\u5c06\u5b83\u7684\u5904\u7406\u7a0b\u5e8f console.log(err) \u6240\u5728\u51fd\u6570\u63a8\u5165\u5fae\u4efb\u52a1\u961f\u5217 2;
\n\u6267\u884c\u5fae\u4efb\u52a1\u961f\u5217 2 \u7684\u5185\u5bb9\uff0c\u6253\u5370 err\uff0c\u5176\u503c\u662f 6;
\n\u6240\u6709\u5fae\u4efb\u52a1\u6267\u884c\u5b8c\u6210\uff0c\u5f00\u59cb\u6267\u884c setTimeout \u91cc\u7684\u5b8f\u4efb\u52a1\uff0c\u6253\u5370 p1\uff0c\u81f3\u6b64\u5168\u90e8\u4ee3\u7801\u6267\u884c\u5b8c\u6210\u3002<\/p>\n

Promise.allSettled()<\/strong><\/span><\/div>\n

Promise.allSettled(iterable) \u5f53\u6240\u6709\u7684\u5b9e\u4f8b\u90fd\u5df2\u7ecf settled\uff0c\u5373\u72b6\u6001\u53d8\u5316\u8fc7\u4e86\uff0c\u90a3\u4e48\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u5b9e\u4f8b\uff0c\u8be5\u65b0\u5b9e\u4f8b\u7684\u5185\u90e8\u503c\u662f\u7531\u6240\u6709\u5b9e\u4f8b\u7684\u503c\u548c\u72b6\u6001\u7ec4\u5408\u6210\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u7684\u6bcf\u9879\u662f\u7531\u6bcf\u4e2a\u5b9e\u4f8b\u7684\u72b6\u6001\u548c\u503c\u7ec4\u6210\u7684\u5bf9\u8c61\u3002<\/p>\n

\r\nfunction init(){ \r\n    return 3 \r\n} \r\nlet p1 = Promise.allSettled([ \r\n    new Promise((resolve, reject) => { \r\n        resolve(9) \r\n    }).then(res => {}), \r\n    new Promise((resolve, reject) => { \r\n        reject(6) \r\n    }), \r\n    init() \r\n]) \r\nlet p2 = p1.then(res => { \r\n    console.log(res) \r\n}, err => { \r\n    console.log(err) \r\n}) \r\n\/\/ [ \r\n\/\/      {status: \"fulfilled\", value: undefined},  \r\n\/\/      {status: \"rejected\", reason: 6},  \r\n\/\/      {status: \"fulfilled\", value: 3} \r\n\/\/ ] \r\n<\/pre>\n

\u53ea\u8981\u6240\u6709\u5b9e\u4f8b\u4e2d\u5305\u542b\u4e00\u4e2a pending \u72b6\u6001\u7684\u5b9e\u4f8b\uff0c\u90a3\u4e48 Promise.allSettled() \u7684\u7ed3\u679c\u4e3a\u8fd4\u56de\u4e00\u4e2a\u8fd9\u6837 Promise { } \u7684\u5b9e\u4f8b\u3002<\/p>\n

\u275dPromise.allSettled() \u662f ES2020 \u4e2d\u65b0\u589e\u7684\u65b9\u6cd5\uff0c\u6240\u4ee5\u6709\u4e00\u4e9b\u6d4f\u89c8\u5668\u53ef\u80fd\u8fd8\u6682\u65f6\u4e0d\u652f\u6301\u3002\u275e<\/p>\n

\u5bf9\u4e8e\u4e0d\u652f\u6301\u7684\u6d4f\u89c8\u5668\uff0c\u53ef\u4ee5\u5199 polyfill\uff1a<\/p>\n

\r\nif(!Promise.allSettled) { \r\n    Promise.allSettled = function(promises) { \r\n        return Promise.all(promises.map(p => Promise.resolve(p) \r\n            .then(value => ({ \r\n                status: 'fulfilled', \r\n                value \r\n            }), reason => ({ \r\n                status: 'rejected', \r\n                reason \r\n            })) \r\n        )); \r\n    } \r\n} \r\n<\/pre>\n

\u611f\u8c22\u9605\u8bfb<\/p>\n

\u9996\u5148\u611f\u8c22\u4f60\u9605\u8bfb\u672c\u6587\uff0c\u76f8\u4fe1\u4f60\u4ed8\u51fa\u7684\u65f6\u95f4\u503c\u5f97\u62e5\u6709\u8fd9\u4efd\u56de\u62a5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6211\u4eec\u90fd\u77e5\u9053 JavaScript \u7684\u4ee3\u7801\u6267\u884c\u7684\u65f6\u5019\u662f\u8dd1\u5728\u5355\u7ebf\u7a0b\u4e0a\u7684\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u53ea\u80fd\u6309\u7167\u4ee3\u7801\u7684\u51fa\u73b0\u987a\u5e8f\uff0c\u4ece\u4e0a\u5230\u4e0b […]<\/p>\n","protected":false},"author":1898,"featured_media":206206,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[620],"class_list":["post-206200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread","tag-promise"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1898"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=206200"}],"version-history":[{"count":7,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206200\/revisions"}],"predecessor-version":[{"id":206208,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/206200\/revisions\/206208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/206206"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=206200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=206200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=206200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}