{"id":52194,"date":"2024-12-03T09:46:37","date_gmt":"2024-12-03T01:46:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52194\/"},"modified":"2024-12-03T09:46:37","modified_gmt":"2024-12-03T01:46:37","slug":"%e5%bc%82%e6%ad%a5%e7%bc%96%e7%a8%8b%e5%9b%9e%e8%b0%83%e3%80%81promise-%e5%92%8c%e5%bc%82%e6%ad%a5%e7%ad%89%e5%be%85","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52194\/","title":{"rendered":"\u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u3001Promise \u548c\u5f02\u6b65\u7b49\u5f85"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u3001Promise \u548c\u5f02\u6b65\u7b49\u5f85<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u3001Promise \u548c\u5f02\u6b65\u7b49\u5f85\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241119\/1732002211673c41a356281.jpg\" class=\"aligncenter\" title=\"\u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u3001Promise \u548c\u5f02\u6b65\u7b49\u5f85\u63d2\u56fe\" alt=\"\u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u3001Promise \u548c\u5f02\u6b65\u7b49\u5f85\u63d2\u56fe\" \/><\/p>\n<p>JavaScript \u4e2d\u7684\u5f02\u6b65\u7f16\u7a0b\u5141\u8bb8\u60a8\u6267\u884c API \u8c03\u7528\u3001\u8bfb\u53d6\u6587\u4ef6\u6216\u67e5\u8be2\u6570\u636e\u5e93\u7b49\u4efb\u52a1\uff0c\u800c\u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u4ee3\u7801\u7684\u6267\u884c\u3002\u8fd9\u5728 JavaScript \u4e2d\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728 Web \u5f00\u53d1\u4e2d\uff0c\u54cd\u5e94\u80fd\u529b\u548c\u6027\u80fd\u662f\u5173\u952e\u3002<\/p>\n<p>\u5173\u952e\u6982\u5ff5<\/p>\n<p><strong>1\u3002\u56de\u8c03\uff1a<\/strong><\/p>\n<p>\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u540e\u6267\u884c\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>\u51fd\u6570 fetchData(\u56de\u8c03) {<br \/> setTimeout(() =&gt; {<br \/> callback(&#8220;\u6570\u636e\u5df2\u83b7\u53d6&#8221;);<br \/> }, 1000);<br \/> }<\/p>\n<p>fetchData((data) =&gt; {<br \/> console.log(\u6570\u636e);<br \/> });<\/p>\n<p><strong>2\u3002\u627f\u8bfa\uff1a<\/strong><\/p>\n<p>\u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u6700\u7ec8\u5b8c\u6210\u6216\u5931\u8d25\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u627f\u8bfa\u53ef\u4ee5\u5904\u4e8e\u4e09\u79cd\u72b6\u6001\u4e4b\u4e00\uff1a\u5f85\u5b9a\u3001\u5df2\u5c65\u884c\u6216\u5df2\u62d2\u7edd\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>\u8ba9promise = new Promise((\u89e3\u51b3, \u62d2\u7edd) =&gt; {<br \/> setTimeout(() =&gt; {<br \/> \u89e3\u6790\uff08\u201c\u6570\u636e\u5df2\u83b7\u53d6\u201d\uff09;<br \/> }, 1000);<br \/> });<\/p>\n<p>\u627f\u8bfa<br \/> .then((data) =&gt; console.log(data))<br \/> .catch((\u9519\u8bef) =&gt; console.log(\u9519\u8bef));<\/p>\n<p><strong>3\u3002\u5f02\u6b65\u548c\u7b49\u5f85\uff1a<\/strong><\/p>\n<p>\u5f02\u6b65\u51fd\u6570\u81ea\u52a8\u8fd4\u56de\u4e00\u4e2a Promise\uff0c\u5e76\u7528\u4e8e\u7b80\u5316 Promise \u7684\u5904\u7406\u3002<\/p>\n<p>await \u6682\u505c\u5f02\u6b65\u51fd\u6570\u7684\u6267\u884c\uff0c\u76f4\u5230 Promise \u5f97\u5230\u89e3\u51b3\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u6613\u4e8e\u9605\u8bfb\u548c\u7f16\u5199\u3002<\/p>\n<p>\u793a\u4f8b\uff1a<\/p>\n<p>\u5f02\u6b65\u51fd\u6570 fetchData() {<br \/> \u5c1d\u8bd5{<br \/> \u8ba9\u6570\u636e = \u7b49\u5f85\u65b0\u7684 Promise((\u89e3\u51b3, \u62d2\u7edd) =&gt; {<br \/> setTimeout(() =&gt; {<br \/> \u89e3\u6790\uff08\u201c\u6570\u636e\u5df2\u83b7\u53d6\u201d\uff09;<br \/> }, 1000);<br \/> });<br \/> console.log(\u6570\u636e);<br \/> } catch (\u9519\u8bef) {<br \/> console.log(\u9519\u8bef);<br \/> }<br \/> }<\/p>\n<p>fetchData();<\/p>\n<p>\u5f02\u6b65\u6a21\u5f0f<\/p>\n<p>\u56de\u8c03\u5730\u72f1\uff1a\u56de\u8c03\u5d4c\u5957\u5728\u5176\u4ed6\u56de\u8c03\u4e2d\u7684\u60c5\u51b5\uff0c\u4f7f\u4ee3\u7801\u96be\u4ee5\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<p>Promise Chaining\uff1a\u4e00\u79cd\u901a\u8fc7\u8fd4\u56de Promise \u5e76\u94fe\u63a5 .then() \u548c .catch() \u65b9\u6cd5\u6765\u907f\u514d\u56de\u8c03\u5730\u72f1\u7684\u6a21\u5f0f\u3002<\/p>\n<p>Async\/Await\uff1a\u4e00\u79cd\u66f4\u73b0\u4ee3\u3001\u66f4\u7b80\u6d01\u7684\u5f02\u6b65\u4ee3\u7801\u7f16\u5199\u65b9\u6cd5\uff0c\u53ef\u4ee5\u907f\u514d Promise \u94fe\u5e76\u4f7f\u4ee3\u7801\u770b\u8d77\u6765\u66f4\u540c\u6b65\u3002<\/p>\n<p>\u7528\u4f8b<\/p>\n<p>API \u8c03\u7528\uff1a\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<p>\u8ba1\u65f6\u5668\uff1a\u4f7f\u7528 setTimeout \u6216 setInterval\u3002<\/p>\n<p>\u6587\u4ef6\u64cd\u4f5c\uff1a\u4ee5\u975e\u963b\u585e\u65b9\u5f0f\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<p>\u4e8b\u4ef6\u5904\u7406\uff1a\u5904\u7406\u70b9\u51fb\u3001\u6309\u952e\u7b49\u4e8b\u4ef6<\/p>\n<p>JavaScript \u4e2d\u7684\u5f02\u6b65\u7f16\u7a0b\u5bf9\u4e8e\u6784\u5efa\u54cd\u5e94\u8fc5\u901f\u3001\u9ad8\u6548\u7684\u5e94\u7528\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\uff0c\u7279\u522b\u662f\u5728\u5904\u7406 I\/O \u5bc6\u96c6\u578b\u64cd\u4f5c\u65f6\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300a\u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u3001Promise \u548c\u5f02\u6b65\u7b49\u5f85\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591a\u6587\u7ae0\u77e5\u8bc6\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u5728\u865a\u62df\u673a\u4e2d\u4e0d\u505c\u673a\u5347\u7ea7\u914d\u7f6e\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u3001Promise \u548c\u5f02\u6b65\u7b49\u5f85 \u6536\u85cf \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300a\u5f02\u6b65\u7f16\u7a0b\u56de\u8c03\u3001Promise \u548c\u5f02\u6b65\u7b49\u5f85\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 JavaScript \u4e2d\u7684\u5f02\u6b65\u7f16\u7a0b\u5141\u8bb8\u60a8\u6267\u884c API \u8c03\u7528\u3001\u8bfb\u53d6\u6587\u4ef6\u6216\u67e5\u8be2\u6570\u636e\u5e93\u7b49\u4efb\u52a1\uff0c\u800c\u4e0d\u4f1a\u963b\u585e\u5176\u4ed6\u4ee3\u7801\u7684\u6267\u884c\u3002\u8fd9\u5728 JavaScript \u4e2d\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728 Web \u5f00\u53d1\u4e2d\uff0c\u54cd\u5e94\u80fd\u529b\u548c\u6027\u80fd\u662f\u5173\u952e\u3002 \u5173\u952e\u6982\u5ff5 1\u3002\u56de\u8c03\uff1a \u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5728\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u540e\u6267\u884c\u3002 \u793a\u4f8b\uff1a \u51fd\u6570 fetchData(\u56de\u8c03) { setTimeout(() =&gt; { callback(&#8220;\u6570\u636e\u5df2\u83b7\u53d6&#8221;); }, 1000); } fetchData((data) =&gt; { console.log(\u6570\u636e); }); 2\u3002\u627f\u8bfa\uff1a \u8868\u793a\u5f02\u6b65\u64cd\u4f5c\u6700\u7ec8\u5b8c\u6210\u6216\u5931\u8d25\u7684\u5bf9\u8c61\u3002 \u627f\u8bfa\u53ef\u4ee5\u5904\u4e8e\u4e09\u79cd\u72b6\u6001\u4e4b\u4e00\uff1a\u5f85\u5b9a\u3001\u5df2\u5c65\u884c\u6216\u5df2\u62d2\u7edd\u3002 \u793a\u4f8b\uff1a \u8ba9promise = new Promise((\u89e3\u51b3, \u62d2\u7edd) =&gt; { setTimeout(() =&gt; { \u89e3\u6790\uff08\u201c\u6570\u636e\u5df2\u83b7\u53d6\u201d\uff09; }, 1000); }); \u627f\u8bfa .then((data) =&gt; console.log(data)) .catch((\u9519\u8bef) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52194","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52194"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52194\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}