{"id":7079,"date":"2023-09-25T09:57:34","date_gmt":"2023-09-25T01:57:34","guid":{"rendered":"https:\/\/i007.vip:15443\/wordpress\/?p=7079"},"modified":"2023-09-25T09:57:34","modified_gmt":"2023-09-25T01:57:34","slug":"node%e5%bc%82%e6%ad%a5%e7%94%a8await%e5%92%8c%e4%b8%8d%e7%94%a8await%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/7079","title":{"rendered":"node\u5f02\u6b65\u7528await\u548c\u4e0d\u7528await\u7684\u533a\u522b"},"content":{"rendered":"<p><a href=\"https:\/\/blog.csdn.net\/foreverling_ling\/article\/details\/80263687\">cc from<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u6700\u8fd1\u5728\u7528node\u5199\u9879\u76ee\u3002\u65b0\u7248node\u5f02\u6b65\u7528\u7684\u662fasync\/await\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\u3002\u6211\u4eec\u90fd\u77e5\u9053\uff0c\u4e00\u822c\u8fd9\u4e24\u4e2a\u5173\u952e\u5b57\u8981\u6210\u5bf9\u51fa\u73b0\u3002\u4f46\u662f\uff0c\u7b14\u8005\u53d1\u73b0\uff0c\u5982\u679c\u4e0d\u9700\u8981\u7b49\u5f85\u8fd4\u56de\u503c\u7684\u8bdd\uff0cawait\u53ef\u4ee5\u4e0d\u52a0\u3002\u90a3\u4e48await\u52a0\u548c\u4e0d\u52a0\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f\u767e\u5ea6\u4ee5\u53cagoogle\u4e86\u5927\u91cf\u8d44\u6599\uff0c\u7ed3\u5408\u8bc4\u8bba\uff0c\u6700\u7ec8\u5728\u5b9e\u8df5\u4e2d\u5f04\u660e\u767d\u4e86\u3002\u4e0b\u9762\u76f4\u63a5\u4e0a\u4f8b\u5b50\u3002<\/p>\n<p>\u4e0d\u52a0await<br \/>\nasync test(ctx,next){<br \/>\nthis.doThing().then(console.log(&#8216;done Thing&#8217;))<br \/>\nthis.doAnotherThing();<br \/>\nconsole.log(&#8216;this way&#8217;);<br \/>\n}<\/p>\n<p>async doThing() {<br \/>\nthis.doA();<br \/>\nthis.doB();<br \/>\n}<\/p>\n<p>doAnotherThing() {<br \/>\nconsole.log(&#8216;do another thing&#8217;)<br \/>\n}<\/p>\n<p>async doA() {<br \/>\nreturn new Promise(resove =&gt; {<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(&#8216;done A&#8217;)<br \/>\nresove()<br \/>\n}, 1000)<br \/>\n})<br \/>\n}<\/p>\n<p>async doB() {<br \/>\nreturn new Promise(resove =&gt; {<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(&#8216;done B&#8217;)<br \/>\nresove()<br \/>\n}, 100)<br \/>\n})<br \/>\n}<br \/>\n\u8fd0\u884ctest\u51fd\u6570\u4ee5\u540e\uff0c\u547d\u4ee4\u884c\u8fc5\u901f\u4f9d\u6b21\u6253\u5370\u4e86\u5982\u4e0b\u7ed3\u679c<\/p>\n<p>\u6211\u4eec\u770b\u5230\uff0c\u6ca1\u6709\u52a0await\uff0c\u5f02\u6b65\u51fd\u6570A,B\u987a\u5e8f\u6267\u884c\uff0c\u7531\u4e8eA\u8fd0\u884c\u65f6\u95f4\u8f83\u957f\uff0c\u6240\u4ee5B\u5148\u6267\u884c\u5b8c\u6210\uff0c\u6574\u4e2a\u8fc7\u7a0b\u6ca1\u6709\u963b\u585e\u3002<\/p>\n<p>\u52a0await<br \/>\nasync test(ctx,next){<br \/>\nthis.doThing().then(console.log(&#8216;done Thing&#8217;))<br \/>\nthis.doAnotherThing();<br \/>\nconsole.log(&#8216;this way&#8217;);<br \/>\n}<\/p>\n<p>async doThing() {<br \/>\nawait this.doA()<br \/>\nawait this.doB()<br \/>\n}<\/p>\n<p>doAnotherThing() {<br \/>\nconsole.log(&#8216;do another thing&#8217;)<br \/>\n}<\/p>\n<p>async doA() {<br \/>\nreturn new Promise(resove =&gt; {<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(&#8216;done A&#8217;)<br \/>\nresove()<br \/>\n}, 1000)<br \/>\n})<br \/>\n}<\/p>\n<p>async doB() {<br \/>\nreturn new Promise(resove =&gt; {<br \/>\nsetTimeout(() =&gt; {<br \/>\nconsole.log(&#8216;done B&#8217;)<br \/>\nresove()<br \/>\n}, 100)<br \/>\n})<br \/>\n}<br \/>\n\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b \uff1a<\/p>\n<p>\u7531\u4e8e\u52a0\u4e86await\uff0c\u6240\u4ee5\u8981\u7b49\u5f85\u5f02\u6b65\u4e8b\u4ef6A\u5148\u5b8c\u6210\uff0c\u7136\u540e\u624d\u4f1a\u8fdb\u884c\u4e8b\u4ef6B\u3002\u4e5f\u5c31\u662fawait\u4e0d\u4f1a\u963b\u585e\u540c\u6b65\u4e8b\u4ef6\u7684\u8fd0\u884c\uff0c\u4f46\u662f\u5f02\u6b65\u5374\u662f\u4e00\u4e2a\u4e00\u4e2a\u6267\u884c\u7684\uff0c\u5176\u4e2d\u4e00\u4e2a\u963b\u585e\uff0c\u4e0b\u4e00\u4e2a\u5f02\u6b65\u4e8b\u4ef6\u5c31\u65e0\u6cd5\u7ee7\u7eed\u3002<\/p>\n<p>\u603b\u7ed3<br \/>\n\u53ef\u89c1\uff0cawait\u4e5f\u5e76\u4e0d\u662f\u6bcf\u6b21\u90fd\u8981\u4f7f\u7528\u3002\u5982\u679c\u4e0d\u9700\u8981\u8fd4\u56de\u503c\u7684\u8bdd\uff0c\u5728\u6267\u884c\u7684\u65f6\u5019\uff0c\u4e0d\u52a0await\u80fd\u591f\u52a0\u5feb\u51fd\u6570\u7684\u8fd0\u884c\u3002<br \/>\n\uff08\u4ee5\u4e0a\u4e3a\u4e2a\u4eba\u89c2\u70b9\uff0c\u5982\u679c\u9519\u8bef\u6216\u8005\u4e0d\u8db3\uff0c\u8bf7\u5927\u5bb6\u6279\u8bc4\u6307\u6b63\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>cc from &nbsp; \u6700\u8fd1\u5728\u7528n<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-7079","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/7079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=7079"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/7079\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=7079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=7079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=7079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}