{"id":1714,"date":"2017-03-26T07:04:45","date_gmt":"2017-03-25T23:04:45","guid":{"rendered":"http:\/\/www.qdabc.cn\/?p=1714"},"modified":"2017-03-26T07:04:45","modified_gmt":"2017-03-25T23:04:45","slug":"%e7%90%86%e8%a7%a3-javascript-%e7%9a%84-asyncawait","status":"publish","type":"post","link":"http:\/\/www.qdabc.cn\/?p=1714","title":{"rendered":"\u7406\u89e3 JavaScript \u7684 async\/await"},"content":{"rendered":"<p>\u968f\u7740 Node 7 \u7684\u53d1\u5e03\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u5f00\u59cb\u7814\u7a76\u636e\u8bf4\u662f\u5f02\u6b65\u7f16\u7a0b\u7ec8\u7ea7\u89e3\u51b3\u65b9\u6848\u7684 async\/await\u3002\u6211\u7b2c\u4e00\u6b21\u770b\u5230\u8fd9\u7ec4\u5173\u952e\u5b57\u5e76\u4e0d\u662f\u5728 JavaScript \u8bed\u8a00\u91cc\uff0c\u800c\u662f\u5728 c# 5.0 \u7684\u8bed\u6cd5\u4e2d\u3002C# \u7684 async\/await \u9700\u8981\u5728 .NET Framework 4.5 \u4ee5\u4e0a\u7684\u7248\u672c\u4e2d\u4f7f\u7528\uff0c\u56e0\u6b64\u6211\u8fd8\u5f88\u60b2\u4f24\u4e86\u4e00\u9635\u4e3a\u4e86\u8981\u517c\u5bb9 XP \u7cfb\u7edf\uff0c\u6211\u4eec\u5f00\u53d1\u7684\u8f6f\u4ef6\u4e0d\u80fd\u4f7f\u7528\u9ad8\u4e8e 4.0 \u7248\u672c\u7684 .NET Framework\u3002<br \/>\n\u6211\u4e4b\u524d\u5728\u300a\u95f2\u8c08\u5f02\u6b65\u8c03\u7528\u201c\u6241\u5e73\u201d\u5316\u300b \u4e2d\u5c31\u8c08\u5230\u4e86\u8fd9\u4e2a\u95ee\u9898\u3002\u65e0\u8bba\u662f\u5728 C# \u8fd8\u662f JavaScript \u4e2d\uff0casync\/await \u90fd\u662f\u975e\u5e38\u68d2\u7684\u7279\u6027\uff0c\u5b83\u4eec\u4e5f\u90fd\u662f\u975e\u5e38\u751c\u7684\u8bed\u6cd5\u7cd6\u3002C# \u7684 async\/await \u5b9e\u73b0\u79bb\u4e0d\u5f00 Task \u6216 Task&lt;Result&gt; \u7c7b\uff0c\u800c JavaScript \u7684 async\/await \u5b9e\u73b0\uff0c\u4e5f\u79bb\u4e0d\u5f00Promise\u3002<br \/>\n\u73b0\u5728\u629b\u5f00 C# \u548c .NET Framework\uff0c\u4e13\u5fc3\u7814\u7a76\u4e0b JavaScript \u7684 async\/await\u3002<br \/>\nasync \u548c await \u5728\u5e72\u4ec0\u4e48<br \/>\n\u4efb\u610f\u4e00\u4e2a\u540d\u79f0\u90fd\u662f\u6709\u610f\u4e49\u7684\uff0c\u5148\u4ece\u5b57\u9762\u610f\u601d\u6765\u7406\u89e3\u3002async \u662f\u201c\u5f02\u6b65\u201d\u7684\u7b80\u5199\uff0c\u800c await \u53ef\u4ee5\u8ba4\u4e3a\u662f async wait \u7684\u7b80\u5199\u3002\u6240\u4ee5\u5e94\u8be5\u5f88\u597d\u7406\u89e3 async \u7528\u4e8e\u7533\u660e\u4e00\u4e2a function \u662f\u5f02\u6b65\u7684\uff0c\u800c await \u7528\u4e8e\u7b49\u5f85\u4e00\u4e2a\u5f02\u6b65\u65b9\u6cd5\u6267\u884c\u5b8c\u6210\u3002<br \/>\n\u53e6\u5916\u8fd8\u6709\u4e00\u4e2a\u5f88\u6709\u610f\u601d\u7684\u8bed\u6cd5\u89c4\u5b9a\uff0cawait \u53ea\u80fd\u51fa\u73b0\u5728 async \u51fd\u6570\u4e2d\u3002\u7136\u540e\u7ec6\u5fc3\u7684\u670b\u53cb\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7591\u95ee\uff0c\u5982\u679c await \u53ea\u80fd\u51fa\u73b0\u5728 async \u51fd\u6570\u4e2d\uff0c\u90a3\u8fd9\u4e2a async \u51fd\u6570\u5e94\u8be5\u600e\u4e48\u8c03\u7528\uff1f<br \/>\n\u5982\u679c\u9700\u8981\u901a\u8fc7 await \u6765\u8c03\u7528\u4e00\u4e2a async \u51fd\u6570\uff0c\u90a3\u8fd9\u4e2a\u8c03\u7528\u7684\u5916\u9762\u5fc5\u987b\u5f97\u518d\u5305\u4e00\u4e2a async \u51fd\u6570\uff0c\u7136\u540e\u2026\u2026\u8fdb\u5165\u6b7b\u5faa\u73af\uff0c\u6c38\u65e0\u51fa\u5934\u4e4b\u65e5\u2026\u2026<br \/>\n\u5982\u679c async \u51fd\u6570\u4e0d\u9700\u8981 await \u6765\u8c03\u7528\uff0c\u90a3 async \u5230\u5e95\u8d77\u4e2a\u5565\u4f5c\u7528\uff1f<br \/>\nasync \u8d77\u4ec0\u4e48\u4f5c\u7528<br \/>\n\u8fd9\u4e2a\u95ee\u9898\u7684\u5173\u952e\u5728\u4e8e\uff0casync \u51fd\u6570\u662f\u600e\u4e48\u5904\u7406\u5b83\u7684\u8fd4\u56de\u503c\u7684\uff01<br \/>\n\u6211\u4eec\u5f53\u7136\u5e0c\u671b\u5b83\u80fd\u76f4\u63a5\u901a\u8fc7 return \u8bed\u53e5\u8fd4\u56de\u6211\u4eec\u60f3\u8981\u7684\u503c\uff0c\u4f46\u662f\u5982\u679c\u771f\u662f\u8fd9\u6837\uff0c\u4f3c\u4e4e\u5c31\u6ca1 await \u4ec0\u4e48\u4e8b\u4e86\u3002\u6240\u4ee5\uff0c\u5199\u6bb5\u4ee3\u7801\u6765\u8bd5\u8bd5\uff0c\u770b\u5b83\u5230\u5e95\u4f1a\u8fd4\u56de\u4ec0\u4e48\uff1a<br \/>\nasync function testAsync() {<\/p>\n<p>&nbsp; &nbsp; return &#8220;hello async&#8221;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>const result = testAsync();<\/p>\n<p>console.log(result);<br \/>\n\u770b\u5230\u8f93\u51fa\u5c31\u604d\u7136\u5927\u609f\u4e86\u8f93\u51fa\u7684\u662f\u4e00\u4e2a Promise \u5bf9\u8c61\u3002<br \/>\nc:\\var\\test&gt; node &#8211;harmony_async_await .<\/p>\n<p>Promise { &#8216;hello async&#8217; }<br \/>\n\u6240\u4ee5\uff0casync \u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a Promise \u5bf9\u8c61\u3002\u4ece\u6587\u6863\u4e2d\u4e5f\u53ef\u4ee5\u5f97\u5230\u8fd9\u4e2a\u4fe1\u606f\u3002async \u51fd\u6570\uff08\u5305\u542b\u51fd\u6570\u8bed\u53e5\u3001\u51fd\u6570\u8868\u8fbe\u5f0f\u3001Lambda\u8868\u8fbe\u5f0f\uff09\u4f1a\u8fd4\u56de\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u5982\u679c\u5728\u51fd\u6570\u4e2d return \u4e00\u4e2a\u76f4\u63a5\u91cf\uff0casync \u4f1a\u628a\u8fd9\u4e2a\u76f4\u63a5\u91cf\u901a\u8fc7 Promise.resolve() \u5c01\u88c5\u6210 Promise \u5bf9\u8c61\u3002<br \/>\nasync \u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u6240\u4ee5\u5728\u6700\u5916\u5c42\u4e0d\u80fd\u7528 await \u83b7\u53d6\u5176\u8fd4\u56de\u503c\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5f53\u7136\u5e94\u8be5\u7528\u539f\u6765\u7684\u65b9\u5f0f\uff1athen() \u94fe\u6765\u5904\u7406\u8fd9\u4e2a Promise \u5bf9\u8c61\uff0c\u5c31\u50cf\u8fd9\u6837<br \/>\ntestAsync().then(v =&gt; {<\/p>\n<p>&nbsp; &nbsp; console.log(v); &nbsp; &nbsp;\/\/ \u8f93\u51fa hello async<\/p>\n<p>});<br \/>\n\u73b0\u5728\u56de\u8fc7\u5934\u6765\u60f3\u4e0b\uff0c\u5982\u679c async \u51fd\u6570\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u53c8\u8be5\u5982\u4f55\uff1f\u5f88\u5bb9\u6613\u60f3\u5230\uff0c\u5b83\u4f1a\u8fd4\u56de Promise.resolve(undefined)\u3002<br \/>\n\u8054\u60f3\u4e00\u4e0b Promise \u7684\u7279\u70b9\u65e0\u7b49\u5f85\uff0c\u6240\u4ee5\u5728\u6ca1\u6709 await \u7684\u60c5\u51b5\u4e0b\u6267\u884c async \u51fd\u6570\uff0c\u5b83\u4f1a\u7acb\u5373\u6267\u884c\uff0c\u8fd4\u56de\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u5e76\u4e14\uff0c\u7edd\u4e0d\u4f1a\u963b\u585e\u540e\u9762\u7684\u8bed\u53e5\u3002\u8fd9\u548c\u666e\u901a\u8fd4\u56de Promise \u5bf9\u8c61\u7684\u51fd\u6570\u5e76\u65e0\u4e8c\u81f4\u3002<br \/>\n\u90a3\u4e48\u4e0b\u4e00\u4e2a\u5173\u952e\u70b9\u5c31\u5728\u4e8e await \u5173\u952e\u5b57\u4e86\u3002<br \/>\nawait \u5230\u5e95\u5728\u7b49\u5565<br \/>\n\u4e00\u822c\u6765\u8bf4\uff0c\u90fd\u8ba4\u4e3a await \u662f\u5728\u7b49\u5f85\u4e00\u4e2a async \u51fd\u6570\u5b8c\u6210\u3002\u4e0d\u8fc7\u6309\u8bed\u6cd5\u8bf4\u660e\uff0cawait \u7b49\u5f85\u7684\u662f\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff0c\u8fd9\u4e2a\u8868\u8fbe\u5f0f\u7684\u8ba1\u7b97\u7ed3\u679c\u662f Promise \u5bf9\u8c61\u6216\u8005\u5176\u5b83\u503c\uff08\u6362\u53e5\u8bdd\u8bf4\uff0c\u5c31\u662f\u6ca1\u6709\u7279\u6b8a\u9650\u5b9a\uff09\u3002<br \/>\n\u56e0\u4e3a async \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u6240\u4ee5 await \u53ef\u4ee5\u7528\u4e8e\u7b49\u5f85\u4e00\u4e2a async \u51fd\u6570\u7684\u8fd4\u56de\u503c\u8fd9\u4e5f\u53ef\u4ee5\u8bf4\u662f await \u5728\u7b49 async \u51fd\u6570\uff0c\u4f46\u8981\u6e05\u695a\uff0c\u5b83\u7b49\u7684\u5b9e\u9645\u662f\u4e00\u4e2a\u8fd4\u56de\u503c\u3002\u6ce8\u610f\u5230 await \u4e0d\u4ec5\u4ec5\u7528\u4e8e\u7b49 Promise \u5bf9\u8c61\uff0c\u5b83\u53ef\u4ee5\u7b49\u4efb\u610f\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\uff0c\u6240\u4ee5\uff0cawait \u540e\u9762\u5b9e\u9645\u662f\u53ef\u4ee5\u63a5\u666e\u901a\u51fd\u6570\u8c03\u7528\u6216\u8005\u76f4\u63a5\u91cf\u7684\u3002\u6240\u4ee5\u4e0b\u9762\u8fd9\u4e2a\u793a\u4f8b\u5b8c\u5168\u53ef\u4ee5\u6b63\u786e\u8fd0\u884c<br \/>\nfunction getSomething() {<\/p>\n<p>&nbsp; &nbsp; return &#8220;something&#8221;;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>async function testAsync() {<\/p>\n<p>&nbsp; &nbsp; return Promise.resolve(&#8220;hello async&#8221;);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>async function test() {<\/p>\n<p>&nbsp; &nbsp; const v1 = await getSomething();<\/p>\n<p>&nbsp; &nbsp; const v2 = await testAsync();<\/p>\n<p>&nbsp; &nbsp; console.log(v1, v2);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>test();<br \/>\nawait \u7b49\u5230\u4e86\u8981\u7b49\u7684\uff0c\u7136\u540e\u5462<br \/>\nawait \u7b49\u5230\u4e86\u5b83\u8981\u7b49\u7684\u4e1c\u897f\uff0c\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u6216\u8005\u5176\u5b83\u503c\uff0c\u7136\u540e\u5462\uff1f\u6211\u4e0d\u5f97\u4e0d\u5148\u8bf4\uff0cawait \u662f\u4e2a\u8fd0\u7b97\u7b26\uff0c\u7528\u4e8e\u7ec4\u6210\u8868\u8fbe\u5f0f\uff0cawait \u8868\u8fbe\u5f0f\u7684\u8fd0\u7b97\u7ed3\u679c\u53d6\u51b3\u4e8e\u5b83\u7b49\u7684\u4e1c\u897f\u3002<br \/>\n\u5982\u679c\u5b83\u7b49\u5230\u7684\u4e0d\u662f\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u90a3 await \u8868\u8fbe\u5f0f\u7684\u8fd0\u7b97\u7ed3\u679c\u5c31\u662f\u5b83\u7b49\u5230\u7684\u4e1c\u897f\u3002<br \/>\n\u5982\u679c\u5b83\u7b49\u5230\u7684\u662f\u4e00\u4e2a Promise \u5bf9\u8c61\uff0cawait \u5c31\u5fd9\u8d77\u6765\u4e86\uff0c\u5b83\u4f1a\u963b\u585e\u540e\u9762\u7684\u4ee3\u7801\uff0c\u7b49\u7740 Promise \u5bf9\u8c61 resolve\uff0c\u7136\u540e\u5f97\u5230 resolve \u7684\u503c\uff0c\u4f5c\u4e3a await \u8868\u8fbe\u5f0f\u7684\u8fd0\u7b97\u7ed3\u679c\u3002<br \/>\n\u770b\u5230\u4e0a\u9762\u7684\u963b\u585e\u4e00\u8bcd\uff0c\u5fc3\u614c\u4e86\u5427\u2026\u2026\u653e\u5fc3\uff0c\u8fd9\u5c31\u662f await \u5fc5\u987b\u7528\u5728 async \u51fd\u6570\u4e2d\u7684\u539f\u56e0\u3002async \u51fd\u6570\u8c03\u7528\u4e0d\u4f1a\u9020\u6210\u963b\u585e\uff0c\u5b83\u5185\u90e8\u6240\u6709\u7684\u963b\u585e\u90fd\u88ab\u5c01\u88c5\u5728\u4e00\u4e2a Promise \u5bf9\u8c61\u4e2d\u5f02\u6b65\u6267\u884c\u3002<br \/>\nasync\/await \u5e2e\u6211\u4eec\u5e72\u4e86\u5565<br \/>\n\u4f5c\u4e2a\u7b80\u5355\u7684\u6bd4\u8f83<br \/>\n\u4e0a\u9762\u5df2\u7ecf\u8bf4\u660e\u4e86 async \u4f1a\u5c06\u5176\u540e\u7684\u51fd\u6570\uff08\u51fd\u6570\u8868\u8fbe\u5f0f\u6216 Lambda\uff09\u7684\u8fd4\u56de\u503c\u5c01\u88c5\u6210\u4e00\u4e2a Promise \u5bf9\u8c61\uff0c\u800c await \u4f1a\u7b49\u5f85\u8fd9\u4e2a Promise \u5b8c\u6210\uff0c\u5e76\u5c06\u5176 resolve \u7684\u7ed3\u679c\u8fd4\u56de\u51fa\u6765\u3002<br \/>\n\u73b0\u5728\u4e3e\u4f8b\uff0c\u7528 setTimeout \u6a21\u62df\u8017\u65f6\u7684\u5f02\u6b65\u64cd\u4f5c\uff0c\u5148\u6765\u770b\u770b\u4e0d\u7528 async\/await \u4f1a\u600e\u4e48\u5199<br \/>\nfunction takeLongTime() {<\/p>\n<p>&nbsp; &nbsp; return new Promise(resolve =&gt; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(() =&gt; resolve(&#8220;long_time_value&#8221;), 1000);<\/p>\n<p>&nbsp; &nbsp; });<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>takeLongTime().then(v =&gt; {<\/p>\n<p>&nbsp; &nbsp; console.log(&#8220;got&#8221;, v);<\/p>\n<p>});<br \/>\n\u5982\u679c\u6539\u7528 async\/await \u5462\uff0c\u4f1a\u662f\u8fd9\u6837<br \/>\nfunction takeLongTime() {<\/p>\n<p>&nbsp; &nbsp; return new Promise(resolve =&gt; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(() =&gt; resolve(&#8220;long_time_value&#8221;), 1000);<\/p>\n<p>&nbsp; &nbsp; });<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>async function test() {<\/p>\n<p>&nbsp; &nbsp; const v = await takeLongTime();<\/p>\n<p>&nbsp; &nbsp; console.log(v);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>test();<br \/>\n\u773c\u5c16\u7684\u540c\u5b66\u5df2\u7ecf\u53d1\u73b0 takeLongTime() \u6ca1\u6709\u7533\u660e\u4e3a async\u3002\u5b9e\u9645\u4e0a\uff0ctakeLongTime() \u672c\u8eab\u5c31\u662f\u8fd4\u56de\u7684 Promise \u5bf9\u8c61\uff0c\u52a0\u4e0d\u52a0 async\u7ed3\u679c\u90fd\u4e00\u6837\uff0c\u5982\u679c\u6ca1\u660e\u767d\uff0c\u8bf7\u56de\u8fc7\u5934\u518d\u53bb\u770b\u770b\u4e0a\u9762\u7684\u201casync \u8d77\u4ec0\u4e48\u4f5c\u7528\u201d\u3002<br \/>\n\u53c8\u4e00\u4e2a\u7591\u95ee\u4ea7\u751f\u4e86\uff0c\u8fd9\u4e24\u6bb5\u4ee3\u7801\uff0c\u4e24\u79cd\u65b9\u5f0f\u5bf9\u5f02\u6b65\u8c03\u7528\u7684\u5904\u7406\uff08\u5b9e\u9645\u5c31\u662f\u5bf9 Promise \u5bf9\u8c61\u7684\u5904\u7406\uff09\u5dee\u522b\u5e76\u4e0d\u660e\u663e\uff0c\u751a\u81f3\u4f7f\u7528 async\/await \u8fd8\u9700\u8981\u591a\u5199\u4e00\u4e9b\u4ee3\u7801\uff0c\u90a3\u5b83\u7684\u4f18\u52bf\u5230\u5e95\u5728\u54ea\uff1f<br \/>\nasync\/await \u7684\u4f18\u52bf\u5728\u4e8e\u5904\u7406 then \u94fe<br \/>\n\u5355\u4e00\u7684 Promise \u94fe\u5e76\u4e0d\u80fd\u53d1\u73b0 async\/await \u7684\u4f18\u52bf\uff0c\u4f46\u662f\uff0c\u5982\u679c\u9700\u8981\u5904\u7406\u7531\u591a\u4e2a Promise \u7ec4\u6210\u7684 then \u94fe\u7684\u65f6\u5019\uff0c\u4f18\u52bf\u5c31\u80fd\u4f53\u73b0\u51fa\u6765\u4e86\uff08\u5f88\u6709\u610f\u601d\uff0cPromise \u901a\u8fc7 then \u94fe\u6765\u89e3\u51b3\u591a\u5c42\u56de\u8c03\u7684\u95ee\u9898\uff0c\u73b0\u5728\u53c8\u7528 async\/await \u6765\u8fdb\u4e00\u6b65\u4f18\u5316\u5b83\uff09\u3002<br \/>\n\u5047\u8bbe\u4e00\u4e2a\u4e1a\u52a1\uff0c\u5206\u591a\u4e2a\u6b65\u9aa4\u5b8c\u6210\uff0c\u6bcf\u4e2a\u6b65\u9aa4\u90fd\u662f\u5f02\u6b65\u7684\uff0c\u800c\u4e14\u4f9d\u8d56\u4e8e\u4e0a\u4e00\u4e2a\u6b65\u9aa4\u7684\u7ed3\u679c\u3002\u6211\u4eec\u4ecd\u7136\u7528 setTimeout \u6765\u6a21\u62df\u5f02\u6b65\u64cd\u4f5c\uff1a<br \/>\n\/**<\/p>\n<p>* \u4f20\u5165\u53c2\u6570 n\uff0c\u8868\u793a\u8fd9\u4e2a\u51fd\u6570\u6267\u884c\u7684\u65f6\u95f4\uff08\u6beb\u79d2\uff09<\/p>\n<p>* \u6267\u884c\u7684\u7ed3\u679c\u662f n + 200\uff0c\u8fd9\u4e2a\u503c\u5c06\u7528\u4e8e\u4e0b\u4e00\u6b65\u9aa4<\/p>\n<p>*\/<\/p>\n<p>function takeLongTime(n) {<\/p>\n<p>&nbsp; &nbsp; return new Promise(resolve =&gt; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(() =&gt; resolve(n + 200), n);<\/p>\n<p>&nbsp; &nbsp; });<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>function step1(n) {<\/p>\n<p>&nbsp; &nbsp; console.log(`step1 with ${n}`);<\/p>\n<p>&nbsp; &nbsp; return takeLongTime(n);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>function step2(n) {<\/p>\n<p>&nbsp; &nbsp; console.log(`step2 with ${n}`);<\/p>\n<p>&nbsp; &nbsp; return takeLongTime(n);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>function step3(n) {<\/p>\n<p>&nbsp; &nbsp; console.log(`step3 with ${n}`);<\/p>\n<p>&nbsp; &nbsp; return takeLongTime(n);<\/p>\n<p>}<br \/>\n\u73b0\u5728\u7528 Promise \u65b9\u5f0f\u6765\u5b9e\u73b0\u8fd9\u4e09\u4e2a\u6b65\u9aa4\u7684\u5904\u7406<br \/>\nfunction doIt() {<\/p>\n<p>&nbsp; &nbsp; console.time(&#8220;doIt&#8221;);<\/p>\n<p>&nbsp; &nbsp; const time1 = 300;<\/p>\n<p>&nbsp; &nbsp; step1(time1)<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; .then(time2 =&gt; step2(time2))<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; .then(time3 =&gt; step3(time3))<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; .then(result =&gt; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`result is ${result}`);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.timeEnd(&#8220;doIt&#8221;);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; });<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>doIt();<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ c:\\var\\test&gt;node &#8211;harmony_async_await .<\/p>\n<p>\/\/ step1 with 300<\/p>\n<p>\/\/ step2 with 500<\/p>\n<p>\/\/ step3 with 700<\/p>\n<p>\/\/ result is 900<\/p>\n<p>\/\/ doIt: 1507.251ms<br \/>\n\u8f93\u51fa\u7ed3\u679c result \u662f step3() \u7684\u53c2\u6570 700 + 200 = 900\u3002doIt() \u987a\u5e8f\u6267\u884c\u4e86\u4e09\u4e2a\u6b65\u9aa4\uff0c\u4e00\u5171\u7528\u4e86 300 + 500 + 700 = 1500 \u6beb\u79d2\uff0c\u548c console.time()\/console.timeEnd() \u8ba1\u7b97\u7684\u7ed3\u679c\u4e00\u81f4\u3002<br \/>\n\u5982\u679c\u7528 async\/await \u6765\u5b9e\u73b0\u5462\uff0c\u4f1a\u662f\u8fd9\u6837<br \/>\nasync function doIt() {<\/p>\n<p>&nbsp; &nbsp; console.time(&#8220;doIt&#8221;);<\/p>\n<p>&nbsp; &nbsp; const time1 = 300;<\/p>\n<p>&nbsp; &nbsp; const time2 = await step1(time1);<\/p>\n<p>&nbsp; &nbsp; const time3 = await step2(time2);<\/p>\n<p>&nbsp; &nbsp; const result = await step3(time3);<\/p>\n<p>&nbsp; &nbsp; console.log(`result is ${result}`);<\/p>\n<p>&nbsp; &nbsp; console.timeEnd(&#8220;doIt&#8221;);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>doIt();<br \/>\n\u7ed3\u679c\u548c\u4e4b\u524d\u7684 Promise \u5b9e\u73b0\u662f\u4e00\u6837\u7684\uff0c\u4f46\u662f\u8fd9\u4e2a\u4ee3\u7801\u770b\u8d77\u6765\u662f\u4e0d\u662f\u6e05\u6670\u5f97\u591a\uff0c\u51e0\u4e4e\u8ddf\u540c\u6b65\u4ee3\u7801\u4e00\u6837<br \/>\n\u8fd8\u6709\u66f4\u9177\u7684<br \/>\n\u73b0\u5728\u628a\u4e1a\u52a1\u8981\u6c42\u6539\u4e00\u4e0b\uff0c\u4ecd\u7136\u662f\u4e09\u4e2a\u6b65\u9aa4\uff0c\u4f46\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u90fd\u9700\u8981\u4e4b\u524d\u6bcf\u4e2a\u6b65\u9aa4\u7684\u7ed3\u679c\u3002<br \/>\nfunction step1(n) {<\/p>\n<p>&nbsp; &nbsp; console.log(`step1 with ${n}`);<\/p>\n<p>&nbsp; &nbsp; return takeLongTime(n);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>function step2(m, n) {<\/p>\n<p>&nbsp; &nbsp; console.log(`step2 with ${m} and ${n}`);<\/p>\n<p>&nbsp; &nbsp; return takeLongTime(m + n);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>function step3(k, m, n) {<\/p>\n<p>&nbsp; &nbsp; console.log(`step3 with ${k}, ${m} and ${n}`);<\/p>\n<p>&nbsp; &nbsp; return takeLongTime(k + m + n);<\/p>\n<p>}<br \/>\n\u8fd9\u56de\u5148\u7528 async\/await \u6765\u5199\uff1a<br \/>\nasync function doIt() {<\/p>\n<p>&nbsp; &nbsp; console.time(&#8220;doIt&#8221;);<\/p>\n<p>&nbsp; &nbsp; const time1 = 300;<\/p>\n<p>&nbsp; &nbsp; const time2 = await step1(time1);<\/p>\n<p>&nbsp; &nbsp; const time3 = await step2(time1, time2);<\/p>\n<p>&nbsp; &nbsp; const result = await step3(time1, time2, time3);<\/p>\n<p>&nbsp; &nbsp; console.log(`result is ${result}`);<\/p>\n<p>&nbsp; &nbsp; console.timeEnd(&#8220;doIt&#8221;);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>doIt();<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ c:\\var\\test&gt;node &#8211;harmony_async_await .<\/p>\n<p>\/\/ step1 with 300<\/p>\n<p>\/\/ step2 with 800 = 300 + 500<\/p>\n<p>\/\/ step3 with 1800 = 300 + 500 + 1000<\/p>\n<p>\/\/ result is 2000<\/p>\n<p>\/\/ doIt: 2907.387ms<br \/>\n\u9664\u4e86\u89c9\u5f97\u6267\u884c\u65f6\u95f4\u53d8\u957f\u4e86\u4e4b\u5916\uff0c\u4f3c\u4e4e\u548c\u4e4b\u524d\u7684\u793a\u4f8b\u6ca1\u5565\u533a\u522b\u554a\uff01\u522b\u6025\uff0c\u8ba4\u771f\u60f3\u60f3\u5982\u679c\u628a\u5b83\u5199\u6210 Promise \u65b9\u5f0f\u5b9e\u73b0\u4f1a\u662f\u4ec0\u4e48\u6837\u5b50\uff1f<br \/>\nfunction doIt() {<\/p>\n<p>&nbsp; &nbsp; console.time(&#8220;doIt&#8221;);<\/p>\n<p>&nbsp; &nbsp; const time1 = 300;<\/p>\n<p>&nbsp; &nbsp; step1(time1)<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; .then(time2 =&gt; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return step2(time1, time2)<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then(time3 =&gt; [time1, time2, time3]);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; })<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; .then(times =&gt; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const [time1, time2, time3] = times;<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return step3(time1, time2, time3);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; })<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; .then(result =&gt; {<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(`result is ${result}`);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.timeEnd(&#8220;doIt&#8221;);<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; });<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>doIt();<br \/>\n\u6709\u6ca1\u6709\u611f\u89c9\u6709\u70b9\u590d\u6742\u7684\u6837\u5b50\uff1f\u90a3\u4e00\u5806\u53c2\u6570\u5904\u7406\uff0c\u5c31\u662f Promise \u65b9\u6848\u7684\u6b7b\u7a74 \u53c2\u6570\u4f20\u9012\u592a\u9ebb\u70e6\u4e86\uff0c\u770b\u7740\u5c31\u6655\uff01<br \/>\n\u6d17\u6d17\u7761\u5427<br \/>\n\u5c31\u76ee\u524d\u6765\u8bf4\uff0c\u5df2\u7ecf\u7406\u89e3 async\/await \u4e86\u5427\uff1f\u4f46\u5176\u5b9e\u8fd8\u6709\u4e00\u4e9b\u4e8b\u60c5\u6ca1\u63d0\u53caPromise \u6709\u53ef\u80fd reject \u554a\uff0c\u600e\u4e48\u5904\u7406\u5462\uff1f\u5982\u679c\u9700\u8981\u5e76\u884c\u5904\u74063\u4e2a\u6b65\u9aa4\uff0c\u518d\u7b49\u5f85\u6240\u6709\u7ed3\u679c\uff0c\u53c8\u8be5\u600e\u4e48\u5904\u7406\u5462\uff1f<\/p>\n<p class=\"post-copyright\">\u6b22\u8fce\u5206\u4eab\u672c\u6587\uff0c\u8f6c\u8f7d\u8bf7\u4fdd\u7559\u51fa\u5904\uff1a<a href=\"http:\/\/www.qdabc.cn\">\u524d\u7aefABC<\/a> &raquo; <a href=\"http:\/\/www.qdabc.cn\/?p=1714\">\u7406\u89e3 JavaScript \u7684 async\/await<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740 Node 7 \u7684\u53d1\u5e03\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u5f00\u59cb\u7814\u7a76\u636e\u8bf4\u662f\u5f02\u6b65\u7f16\u7a0b\u7ec8\u7ea7\u89e3\u51b3\u65b9\u6848\u7684 async\/await\u3002\u6211\u7b2c\u4e00\u6b21\u770b\u5230\u8fd9\u7ec4\u5173\u952e\u5b57\u5e76\u4e0d\u662f\u5728 JavaScript \u8bed\u8a00\u91cc\uff0c\u800c\u662f\u5728 c# 5.0 \u7684\u8bed\u6cd5\u4e2d\u3002C# [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9],"_links":{"self":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1714"}],"collection":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1714"}],"version-history":[{"count":1,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1714\/revisions"}],"predecessor-version":[{"id":1715,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=\/wp\/v2\/posts\/1714\/revisions\/1715"}],"wp:attachment":[{"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1714"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.qdabc.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}