{"id":17010,"date":"2025-03-31T16:27:07","date_gmt":"2025-03-31T08:27:07","guid":{"rendered":"https:\/\/92it.top\/?p=17010"},"modified":"2025-03-31T16:27:27","modified_gmt":"2025-03-31T08:27:27","slug":"javascript-promise","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=17010","title":{"rendered":"JavaScript Promise"},"content":{"rendered":"\n<p>Promise \u662f\u4e00\u4e2a ECMAScript 6 \u63d0\u4f9b\u7684\u7c7b\uff0c\u76ee\u7684\u662f\u66f4\u52a0\u4f18\u96c5\u5730\u4e66\u5199\u590d\u6742\u7684\u5f02\u6b65\u4efb\u52a1\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e Promise \u662f ES6 \u65b0\u589e\u52a0\u7684\uff0c\u6240\u4ee5\u4e00\u4e9b\u65e7\u7684\u6d4f\u89c8\u5668\u5e76\u4e0d\u652f\u6301\uff0c\u82f9\u679c\u7684 Safari 10 \u548c Windows \u7684 Edge 14 \u7248\u672c\u4ee5\u4e0a\u6d4f\u89c8\u5668\u624d\u5f00\u59cb\u652f\u6301 ES6 \u7279\u6027\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f Promise \u6d4f\u89c8\u5668\u652f\u6301\u7684\u60c5\u51b5\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"84\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-1024x84.png\" alt=\"\" class=\"wp-image-17011\" style=\"width:540px;height:auto\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-1024x84.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-300x24.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-768x63.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-1536x125.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-2048x167.png 2048w, https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-830x68.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-230x19.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-350x29.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2025\/03\/image-1-480x39.png 480w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n<p><strong>\u6784\u9020 Promise<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u65b0\u5efa\u4e00\u4e2a Promise \u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Promise(function (resolve, reject) {\n    \/\/ \u8981\u505a\u7684\u4e8b\u60c5...\n});<\/pre>\n\n\n\n<p>\u901a\u8fc7\u65b0\u5efa\u4e00\u4e2a Promise \u5bf9\u8c61\u597d\u50cf\u5e76\u6ca1\u6709\u770b\u51fa\u5b83\u600e\u6837 &#8220;\u66f4\u52a0\u4f18\u96c5\u5730\u4e66\u5199\u590d\u6742\u7684\u5f02\u6b65\u4efb\u52a1&#8221;\u3002\u6211\u4eec\u4e4b\u524d\u9047\u5230\u7684\u5f02\u6b65\u4efb\u52a1\u90fd\u662f\u4e00\u6b21\u5f02\u6b65\uff0c\u5982\u679c\u9700\u8981\u591a\u6b21\u8c03\u7528\u5f02\u6b65\u51fd\u6570\u5462\uff1f\u4f8b\u5982\uff0c\u5982\u679c\u6211\u60f3\u5206\u4e09\u6b21\u8f93\u51fa\u5b57\u7b26\u4e32\uff0c\u7b2c\u4e00\u6b21\u95f4\u9694 1 \u79d2\uff0c\u7b2c\u4e8c\u6b21\u95f4\u9694 4 \u79d2\uff0c\u7b2c\u4e09\u6b21\u95f4\u9694 3 \u79d2\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">setTimeout(function () {\n    console.log(\"First\");\n    setTimeout(function () {\n        console.log(\"Second\");\n        setTimeout(function () {\n            console.log(\"Third\");\n        }, 3000);\n    }, 4000);\n}, 1000);<\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u7a0b\u5e8f\u5b9e\u73b0\u4e86\u8fd9\u4e2a\u529f\u80fd\uff0c\u4f46\u662f\u5b83\u662f\u7528 &#8220;\u51fd\u6570\u7011\u5e03&#8221; \u6765\u5b9e\u73b0\u7684\u3002\u53ef\u60f3\u800c\u77e5\uff0c\u5728\u4e00\u4e2a\u590d\u6742\u7684\u7a0b\u5e8f\u5f53\u4e2d\uff0c\u7528 &#8220;\u51fd\u6570\u7011\u5e03&#8221; \u5b9e\u73b0\u7684\u7a0b\u5e8f\u65e0\u8bba\u662f\u7ef4\u62a4\u8fd8\u662f\u5f02\u5e38\u5904\u7406\u90fd\u662f\u4e00\u4ef6\u7279\u522b\u7e41\u7410\u7684\u4e8b\u60c5\uff0c\u800c\u4e14\u4f1a\u8ba9\u7f29\u8fdb\u683c\u5f0f\u53d8\u5f97\u975e\u5e38\u5197\u8d58\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u7528 Promise \u6765\u5b9e\u73b0\u540c\u6837\u7684\u529f\u80fd\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Promise(function (resolve, reject) {\n    setTimeout(function () {\n        console.log(\"First\");\n        resolve();\n    }, 1000);\n}).then(function () {\n    return new Promise(function (resolve, reject) {\n        setTimeout(function () {\n            console.log(\"Second\");\n            resolve();\n        }, 4000);\n    });\n}).then(function () {\n    setTimeout(function () {\n        console.log(\"Third\");\n    }, 3000);\n});<\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u8f83\u957f\uff0c\u6240\u4ee5\u8fd8\u4e0d\u9700\u8981\u5b8c\u5168\u7406\u89e3\u5b83\uff0c\u6211\u60f3\u5f15\u8d77\u6ce8\u610f\u7684\u662f Promise \u5c06\u5d4c\u5957\u683c\u5f0f\u7684\u4ee3\u7801\u53d8\u6210\u4e86\u987a\u5e8f\u683c\u5f0f\u7684\u4ee3\u7801\u3002<\/p>\n\n\n\n<p><strong>Promise \u7684\u6784\u9020\u51fd\u6570<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>Promise \u6784\u9020\u51fd\u6570\u662f JavaScript \u4e2d\u7528\u4e8e\u521b\u5efa Promise \u5bf9\u8c61\u7684\u5185\u7f6e\u6784\u9020\u51fd\u6570\u3002<\/p>\n\n\n\n<p>Promise \u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u8be5\u51fd\u6570\u662f\u540c\u6b65\u7684\u5e76\u4e14\u4f1a\u88ab\u7acb\u5373\u6267\u884c\uff0c\u6240\u4ee5\u6211\u4eec\u79f0\u4e4b\u4e3a\u8d77\u59cb\u51fd\u6570\u3002\u8d77\u59cb\u51fd\u6570\u5305\u542b\u4e24\u4e2a\u53c2\u6570 resolve \u548c reject\uff0c\u5206\u522b\u8868\u793a Promise \u6210\u529f\u548c\u5931\u8d25\u7684\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u8d77\u59cb\u51fd\u6570\u6267\u884c\u6210\u529f\u65f6\uff0c\u5b83\u5e94\u8be5\u8c03\u7528 resolve \u51fd\u6570\u5e76\u4f20\u9012\u6210\u529f\u7684\u7ed3\u679c\u3002\u5f53\u8d77\u59cb\u51fd\u6570\u6267\u884c\u5931\u8d25\u65f6\uff0c\u5b83\u5e94\u8be5\u8c03\u7528 reject \u51fd\u6570\u5e76\u4f20\u9012\u5931\u8d25\u7684\u539f\u56e0\u3002<\/p>\n\n\n\n<p>Promise \u6784\u9020\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5177\u6709\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul>\n<li>then\uff1a\u7528\u4e8e\u5904\u7406 Promise \u6210\u529f\u72b6\u6001\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li>catch\uff1a\u7528\u4e8e\u5904\u7406 Promise \u5931\u8d25\u72b6\u6001\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n\n\n\n<li>finally\uff1a\u65e0\u8bba Promise \u662f\u6210\u529f\u8fd8\u662f\u5931\u8d25\uff0c\u90fd\u4f1a\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528 Promise \u6784\u9020\u51fd\u6570\u521b\u5efa Promise \u5bf9\u8c61\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p>\u5f53 Promise \u88ab\u6784\u9020\u65f6\uff0c\u8d77\u59cb\u51fd\u6570\u4f1a\u88ab\u540c\u6b65\u6267\u884c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">const promise = new Promise((resolve, reject) => {\n  \/\/ \u5f02\u6b65\u64cd\u4f5c\n  setTimeout(() => {\n    if (Math.random() &lt; 0.5) {\n      resolve('success');\n    } else {\n      reject('error');\n    }\n  }, 1000);\n});\n \npromise.then(result => {\n  console.log(result);\n}).catch(error => {\n  console.log(error);\n});<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 Promise \u6784\u9020\u51fd\u6570\u521b\u5efa\u4e86\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528 setTimeout \u6a21\u62df\u4e86\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u3002\u5982\u679c\u5f02\u6b65\u64cd\u4f5c\u6210\u529f\uff0c\u5219\u8c03\u7528 resolve \u51fd\u6570\u5e76\u4f20\u9012\u6210\u529f\u7684\u7ed3\u679c\uff1b\u5982\u679c\u5f02\u6b65\u64cd\u4f5c\u5931\u8d25\uff0c\u5219\u8c03\u7528 reject \u51fd\u6570\u5e76\u4f20\u9012\u5931\u8d25\u7684\u539f\u56e0\u3002\u7136\u540e\u6211\u4eec\u4f7f\u7528 then \u65b9\u6cd5\u5904\u7406 Promise \u6210\u529f\u72b6\u6001\u7684\u56de\u8c03\u51fd\u6570\uff0c\u4f7f\u7528 catch \u65b9\u6cd5\u5904\u7406 Promise \u5931\u8d25\u72b6\u6001\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u8fd9\u6bb5\u7a0b\u5e8f\u4f1a\u76f4\u63a5\u8f93\u51fa <strong>error<\/strong> \u6216 <strong>success<\/strong>\u3002<\/p>\n\n\n\n<p>resolve \u548c reject \u90fd\u662f\u51fd\u6570\uff0c\u5176\u4e2d\u8c03\u7528 resolve \u4ee3\u8868\u4e00\u5207\u6b63\u5e38\uff0creject \u662f\u51fa\u73b0\u5f02\u5e38\u65f6\u6240\u8c03\u7528\u7684\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Promise(function (resolve, reject) {\n    var a = 0;\n    var b = 1;\n    if (b == 0) reject(\"Divide zero\");\n    else resolve(a \/ b);\n}).then(function (value) {\n    console.log(\"a \/ b = \" + value);\n}).catch(function (err) {\n    console.log(err);\n}).finally(function () {\n    console.log(\"End\");\n});<\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u7a0b\u5e8f\u6267\u884c\u7ed3\u679c\u662f:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">a \/ b = 0\nEnd<\/pre>\n\n\n\n<p>Promise \u7c7b\u6709 .then() .catch() \u548c .finally() \u4e09\u4e2a\u65b9\u6cd5\uff0c\u8fd9\u4e09\u4e2a\u65b9\u6cd5\u7684\u53c2\u6570\u90fd\u662f\u4e00\u4e2a\u51fd\u6570\uff0c.then() \u53ef\u4ee5\u5c06\u53c2\u6570\u4e2d\u7684\u51fd\u6570\u6dfb\u52a0\u5230\u5f53\u524d Promise \u7684\u6b63\u5e38\u6267\u884c\u5e8f\u5217\uff0c.catch() \u5219\u662f\u8bbe\u5b9a Promise \u7684\u5f02\u5e38\u5904\u7406\u5e8f\u5217\uff0c.finally() \u662f\u5728 Promise \u6267\u884c\u7684\u6700\u540e\u4e00\u5b9a\u4f1a\u6267\u884c\u7684\u5e8f\u5217\u3002 .then() \u4f20\u5165\u7684\u51fd\u6570\u4f1a\u6309\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\uff0c\u6709\u4efb\u4f55\u5f02\u5e38\u90fd\u4f1a\u76f4\u63a5\u8df3\u5230 catch \u5e8f\u5217\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">new Promise(function (resolve, reject) {\n    console.log(1111);\n    resolve(2222);\n}).then(function (value) {\n    console.log(value);\n    return 3333;\n}).then(function (value) {\n    console.log(value);\n    throw \"An error\";\n}).catch(function (err) {\n    console.log(err);\n});<\/pre>\n\n\n\n<p>\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">1111\n2222\n3333\nAn error<\/pre>\n\n\n\n<p>resolve() \u4e2d\u53ef\u4ee5\u653e\u7f6e\u4e00\u4e2a\u53c2\u6570\u7528\u4e8e\u5411\u4e0b\u4e00\u4e2a then \u4f20\u9012\u4e00\u4e2a\u503c\uff0cthen \u4e2d\u7684\u51fd\u6570\u4e5f\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u503c\u4f20\u9012\u7ed9 then\u3002\u4f46\u662f\uff0c\u5982\u679c then \u4e2d\u8fd4\u56de\u7684\u662f\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u90a3\u4e48\u4e0b\u4e00\u4e2a then \u5c06\u76f8\u5f53\u4e8e\u5bf9\u8fd9\u4e2a\u8fd4\u56de\u7684 Promise \u8fdb\u884c\u64cd\u4f5c\uff0c\u8fd9\u4e00\u70b9\u4ece\u521a\u624d\u7684\u8ba1\u65f6\u5668\u7684\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u51fa\u6765\u3002<\/p>\n\n\n\n<p>reject() \u53c2\u6570\u4e2d\u4e00\u822c\u4f1a\u4f20\u9012\u4e00\u4e2a\u5f02\u5e38\u7ed9\u4e4b\u540e\u7684 catch \u51fd\u6570\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u8bf7\u6ce8\u610f\u4ee5\u4e0b\u4e24\u70b9\uff1a<\/p>\n\n\n\n<ul>\n<li>resolve \u548c reject \u7684\u4f5c\u7528\u57df\u53ea\u6709\u8d77\u59cb\u51fd\u6570\uff0c\u4e0d\u5305\u62ec then \u4ee5\u53ca\u5176\u4ed6\u5e8f\u5217\uff1b<\/li>\n\n\n\n<li>resolve \u548c reject \u5e76\u4e0d\u80fd\u591f\u4f7f\u8d77\u59cb\u51fd\u6570\u505c\u6b62\u8fd0\u884c\uff0c\u522b\u5fd8\u4e86 return\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>Promise \u51fd\u6570<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0a\u8ff0\u7684 &#8220;\u8ba1\u65f6\u5668&#8221; \u7a0b\u5e8f\u770b\u4e0a\u53bb\u6bd4\u51fd\u6570\u7011\u5e03\u8fd8\u8981\u957f\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u7684\u6838\u5fc3\u90e8\u5206\u5199\u6210\u4e00\u4e2a Promise \u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function print(delay, message) {\n    return new Promise(function (resolve, reject) {\n        setTimeout(function () {\n            console.log(message);\n            resolve();\n        }, delay);\n    });\n}<\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u653e\u5fc3\u5927\u80c6\u7684\u5b9e\u73b0\u7a0b\u5e8f\u529f\u80fd\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(1000, \"First\").then(function () {\n    return print(4000, \"Second\");\n}).then(function () {\n    print(3000, \"Third\");\n});<\/pre>\n\n\n\n<p>\u8fd9\u79cd\u8fd4\u56de\u503c\u4e3a\u4e00\u4e2a Promise \u5bf9\u8c61\u7684\u51fd\u6570\u79f0\u4f5c Promise \u51fd\u6570\uff0c\u5b83\u5e38\u5e38\u7528\u4e8e\u5f00\u53d1\u57fa\u4e8e\u5f02\u6b65\u64cd\u4f5c\u7684\u5e93\u3002<\/p>\n\n\n\n<p><strong>\u56de\u7b54\u5e38\u89c1\u7684\u95ee\u9898\uff08FAQ\uff09<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Q: then\u3001catch \u548c finally \u5e8f\u5217\u80fd\u5426\u987a\u5e8f\u98a0\u5012\uff1f<\/strong><\/p>\n\n\n\n<p>A: \u53ef\u4ee5\uff0c\u6548\u679c\u5b8c\u5168\u4e00\u6837\u3002\u4f46\u4e0d\u5efa\u8bae\u8fd9\u6837\u505a\uff0c\u6700\u597d\u6309 then-catch-finally \u7684\u987a\u5e8f\u7f16\u5199\u7a0b\u5e8f\u3002<\/p>\n\n\n\n<p><strong>Q: \u9664\u4e86 then \u5757\u4ee5\u5916\uff0c\u5176\u5b83\u4e24\u79cd\u5757\u80fd\u5426\u591a\u6b21\u4f7f\u7528\uff1f<\/strong><\/p>\n\n\n\n<p>A: \u53ef\u4ee5\uff0cfinally \u4e0e then \u4e00\u6837\u4f1a\u6309\u987a\u5e8f\u6267\u884c\uff0c\u4f46\u662f catch \u5757\u53ea\u4f1a\u6267\u884c\u7b2c\u4e00\u4e2a\uff0c\u9664\u975e catch \u5757\u91cc\u6709\u5f02\u5e38\u3002\u6240\u4ee5\u6700\u597d\u53ea\u5b89\u6392\u4e00\u4e2a catch \u548c finally \u5757\u3002<\/p>\n\n\n\n<p><strong>Q: then \u5757\u5982\u4f55\u4e2d\u65ad\uff1f<\/strong><\/p>\n\n\n\n<p>A: then \u5757\u9ed8\u8ba4\u4f1a\u5411\u4e0b\u987a\u5e8f\u6267\u884c\uff0creturn \u662f\u4e0d\u80fd\u4e2d\u65ad\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7 throw \u6765\u8df3\u8f6c\u81f3 catch \u5b9e\u73b0\u4e2d\u65ad\u3002<\/p>\n\n\n\n<p><strong>Q: \u4ec0\u4e48\u65f6\u5019\u9002\u5408\u7528 Promise \u800c\u4e0d\u662f\u4f20\u7edf\u56de\u8c03\u51fd\u6570\uff1f<\/strong><\/p>\n\n\n\n<p>A: \u5f53\u9700\u8981\u591a\u6b21\u987a\u5e8f\u6267\u884c\u5f02\u6b65\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u901a\u8fc7\u5f02\u6b65\u65b9\u6cd5\u5148\u540e\u68c0\u6d4b\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u9700\u8981\u5148\u5f02\u6b65\u68c0\u6d4b\u7528\u6237\u540d\uff0c\u7136\u540e\u518d\u5f02\u6b65\u68c0\u6d4b\u5bc6\u7801\u7684\u60c5\u51b5\u4e0b\u5c31\u5f88\u9002\u5408 Promise\u3002<\/p>\n\n\n\n<p><strong>Q: Promise \u662f\u4e00\u79cd\u5c06\u5f02\u6b65\u8f6c\u6362\u4e3a\u540c\u6b65\u7684\u65b9\u6cd5\u5417\uff1f<\/strong><\/p>\n\n\n\n<p>A: \u5b8c\u5168\u4e0d\u662f\u3002Promise \u53ea\u4e0d\u8fc7\u662f\u4e00\u79cd\u66f4\u826f\u597d\u7684\u7f16\u7a0b\u98ce\u683c\u3002<\/p>\n\n\n\n<p><strong>Q: \u4ec0\u4e48\u65f6\u5019\u6211\u4eec\u9700\u8981\u518d\u5199\u4e00\u4e2a then \u800c\u4e0d\u662f\u5728\u5f53\u524d\u7684 then \u63a5\u7740\u7f16\u7a0b\uff1f<\/strong><\/p>\n\n\n\n<p>A: \u5f53\u4f60\u53c8\u9700\u8981\u8c03\u7528\u4e00\u4e2a\u5f02\u6b65\u4efb\u52a1\u7684\u65f6\u5019\u3002<\/p>\n\n\n\n<p><strong>\u5f02\u6b65\u51fd\u6570<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5f02\u6b65\u51fd\u6570\uff08async function\uff09\u662f ECMAScript 2017 (ECMA-262) \u6807\u51c6\u7684\u89c4\u8303\uff0c\u51e0\u4e4e\u88ab\u6240\u6709\u6d4f\u89c8\u5668\u6240\u652f\u6301\uff0c\u9664\u4e86 Internet Explorer\u3002<\/p>\n\n\n\n<p>\u5728 Promise \u4e2d\u6211\u4eec\u7f16\u5199\u8fc7\u4e00\u4e2a Promise \u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function print(delay, message) {\n    return new Promise(function (resolve, reject) {\n        setTimeout(function () {\n            console.log(message);\n            resolve();\n        }, delay);\n    });\n}<\/pre>\n\n\n\n<p>\u7136\u540e\u7528\u4e0d\u540c\u7684\u65f6\u95f4\u95f4\u9694\u8f93\u51fa\u4e86\u4e09\u884c\u6587\u672c\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print(1000, \"First\").then(function () {\n    return print(4000, \"Second\");\n}).then(function () {\n    print(3000, \"Third\");\n});<\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u6bb5\u4ee3\u7801\u53d8\u5f97\u66f4\u597d\u770b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">async function asyncFunc() {\n    await print(1000, \"First\");\n    await print(4000, \"Second\");\n    await print(3000, \"Third\");\n}\nasyncFunc();<\/pre>\n\n\n\n<p>\u54c8\uff01\u8fd9\u5c82\u4e0d\u662f\u5c06\u5f02\u6b65\u64cd\u4f5c\u53d8\u5f97\u50cf\u540c\u6b65\u64cd\u4f5c\u4e00\u6837\u5bb9\u6613\u4e86\u5417\uff01<\/p>\n\n\n\n<p>\u8fd9\u6b21\u7684\u56de\u7b54\u662f\u80af\u5b9a\u7684\uff0c\u5f02\u6b65\u51fd\u6570 async function \u4e2d\u53ef\u4ee5\u4f7f\u7528 await \u6307\u4ee4\uff0cawait \u6307\u4ee4\u540e\u5fc5\u987b\u8ddf\u7740\u4e00\u4e2a Promise\uff0c\u5f02\u6b65\u51fd\u6570\u4f1a\u5728\u8fd9\u4e2a Promise \u8fd0\u884c\u4e2d\u6682\u505c\uff0c\u76f4\u5230\u5176\u8fd0\u884c\u7ed3\u675f\u518d\u7ee7\u7eed\u8fd0\u884c\u3002<\/p>\n\n\n\n<p>\u5f02\u6b65\u51fd\u6570\u5b9e\u9645\u4e0a\u539f\u7406\u4e0e Promise \u539f\u751f API \u7684\u673a\u5236\u662f\u4e00\u6a21\u4e00\u6837\u7684\uff0c\u53ea\u4e0d\u8fc7\u66f4\u4fbf\u4e8e\u7a0b\u5e8f\u5458\u9605\u8bfb\u3002<\/p>\n\n\n\n<p>\u5904\u7406\u5f02\u5e38\u7684\u673a\u5236\u5c06\u7528 try-catch \u5757\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">async function asyncFunc() {\n    try {\n        await new Promise(function (resolve, reject) {\n            throw \"Some error\"; \/\/ \u6216\u8005 reject(\"Some error\")\n        });\n    } catch (err) {\n        console.log(err);\n        \/\/ \u4f1a\u8f93\u51fa Some error\n    }\n}\nasyncFunc();<\/pre>\n\n\n\n<p>\u5982\u679c Promise \u6709\u4e00\u4e2a\u6b63\u5e38\u7684\u8fd4\u56de\u503c\uff0cawait \u8bed\u53e5\u4e5f\u4f1a\u8fd4\u56de\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">async function asyncFunc() {\n    let value = await new Promise(\n        function (resolve, reject) {\n            resolve(\"Return value\");\n        }\n    );\n    console.log(value);\n}\nasyncFunc();<\/pre>\n\n\n\n<p>\u7a0b\u5e8f\u4f1a\u8f93\u51fa:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Return value<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Promise \u662f\u4e00\u4e2a ECMAScript 6 \u63d0\u4f9b\u7684\u7c7b\uff0c\u76ee\u7684\u662f\u66f4\u52a0\u4f18\u96c5\u5730\u4e66\u5199\u590d\u6742\u7684\u5f02\u6b65\u4efb\u52a1\u3002 \u7531\u4e8e Pro [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17010"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17010"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17010\/revisions"}],"predecessor-version":[{"id":17014,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/17010\/revisions\/17014"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}