{"id":5657,"date":"2017-02-08T17:06:37","date_gmt":"2017-02-08T09:06:37","guid":{"rendered":"https:\/\/sdeno.com\/?p=5657"},"modified":"2019-01-04T17:07:53","modified_gmt":"2019-01-04T09:07:53","slug":"asyncawait","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=5657","title":{"rendered":"async\/await\u7b14\u8bb0"},"content":{"rendered":"<p>es7\u8bed\u6cd5\uff0c\u4e3b\u8981\u4f5c\u7528\u8ba9\u5f02\u6b65\u64cd\u4f5c\u66f4\u7b80\u5355\uff0c\u5728Generator\u51fd\u6570\u57fa\u7840\u4e0a\u4f18\u5316\u5f97\u6765<\/p>\n<p>&nbsp;<\/p>\n<p>Generator \u51fd\u6570<\/p>\n<pre>var gen = function* () {\r\n  var f1 = yield readFile('\/etc\/fstab');\r\n  var f2 = yield readFile('\/etc\/shells');\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8c03\u7528\uff1a<\/p>\n<pre>gen().next()\r\ngen().next()\r\ngen().next()\r\n...<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>async\u51fd\u6570<\/p>\n<pre>var asyncReadFile = async function (){\r\n  var f1 = await readFile('\/etc\/fstab');\r\n  var f2 = await readFile('\/etc\/shells');\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<p>\u8c03\u7528\uff1a<\/p>\n<pre>asyncReadFile()<\/pre>\n<p>&nbsp;<\/p>\n<p>async\u51fd\u6570\u5c31\u662f\u5c06 Generator \u51fd\u6570\u7684\u661f\u53f7\uff08*\uff09\u66ff\u6362\u6210async\uff0c\u5c06yield\u66ff\u6362\u6210await\uff0c\u4ec5\u6b64\u800c\u5df2\u3002<br \/>\n\u8c03\u7528\u7684\u65b9\u6cd5\u4e5f\u660e\u663e\u4e0d\u540c\uff0cGenerator\u9700\u8981\u4f7f\u7528next()\u6765\u4e00\u4e2a\u4e2a\u8c03\u7528\uff0c\u800casync\u8ddf\u666e\u901a\u51fd\u6570\u8c03\u7528\u4e00\u6837<\/p>\n<p><a href=\"http:\/\/es6.ruanyifeng.com\/?search=await&amp;x=5&amp;y=7#docs\/async\" target=\"_blank\" rel=\"noopener\">http:\/\/es6.ruanyifeng.com\/?search=await&amp;x=5&amp;y=7#docs\/async\u00a0<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>async\/await\u5176\u5b9e\u662fGenerator\u51fd\u6570\u7684\u8bed\u6cd5\u7cd6\u3002\u5f02\u6b65\u7f16\u7a0b\u53ef\u4ee5\u4f7f\u7528async\/await\u914d\u5408Promise\u5bf9\u8c61\u6765\u5b9e\u73b0\uff0c\u8bed\u6cd5\u6613\u8bfb\u3002<\/p>\n<p>\u8981\u7406\u89e3async\/await\u5728js\u4e2d\u7684\u6267\u884c\u987a\u5e8f\uff0c\u6ca1\u6709\u4e00\u70b9\u57fa\u7840\u7406\u8bba\u652f\u6301\u662f\u5f88\u96be\u7406\u89e3\u7684\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>1\uff0casync \u7528\u4e8e\u7533\u660e\u4e00\u4e2a function \u662f\u5f02\u6b65\u7684\uff0c\u6240\u4ee5\u5728\u8be5function\u91cc\u9762\u7684\u7a0b\u5e8f\u90fd\u662f\u5f02\u6b65\u7684\uff0c\u4f46\u6709\u65f6\u4e5f\u9700\u8981\u4f9d\u8d56\u67d0\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6210\u5f97\u5230\u7ed3\u679c\u624d\u80fd\u7ee7 \u7eed\u6267\u884c\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u5728\u51fd\u6570\u4f53\u91cc\u9762\u4f7f\u7528await<\/p>\n<p>2\uff0cawait \u53ea\u80fd\u51fa\u73b0\u5728 async \u51fd\u6570\u4e2d<\/p>\n<p>3\uff0cawait\u540e\u9762\u53ef\u4ee5\u63a5\u7740\u4e00\u4e2a\u76f4\u63a5\u53d8\u91cf\u6216\u8005\u662f\u4e00\u4e2apromise\u5bf9\u8c61<\/p>\n<p>4\uff0cawait\u540e\u9762\u63a5\u7740\u662fpromise\u5bf9\u8c61\uff0casync\u51fd\u6570\u4f53\u5185\u5c31\u4f1a\u963b\u585e\u8981\u7b49await\u540e\u9762\u7684promise\u51fd\u6570\u6267\u884c\u5b8c\u6210\u624d\u4f1a\u7ee7\u7eed\u5f80\u4e0b\u8d70\u3002async\u51fd\u6570\u4f53\u5916\u7684\u4ee3\u7801\u53ef\u4e0d \u4f1a\u53bb\u4e00\u76f4\u7b49\u5f85\u3002<\/p>\n<p>5\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<\/p>\n<p>6\uff0c\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\u76f4\u63a5new Promise \u5bf9\u8c61\u6548\u679c\u4e00\u6837\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5173\u4e8easync\u66f4\u591a\u4e86\u89e3\uff0c\u67e5\u770b\u6b64\u6848\u4f8b\u66f4\u76f4\u63a5\uff1a<a href=\"https:\/\/sdeno.com\/wp-content\/uploads\/2017\/02\/async.html\">async.html<\/a><\/p>\n<p><a href=\"https:\/\/sdeno.com\/wp-content\/uploads\/2017\/02\/1.html\">\u4fee\u6539\u540easync.html<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>es7\u8bed\u6cd5\uff0c\u4e3b\u8981\u4f5c\u7528\u8ba9\u5f02\u6b65\u64cd\u4f5c\u66f4\u7b80\u5355\uff0c\u5728Generator\u51fd\u6570\u57fa\u7840\u4e0a\u4f18\u5316\u5f97\u6765 &nbsp; Generator [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,11],"tags":[],"class_list":["post-5657","post","type-post","status-publish","format-standard","hentry","category-javascript","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5657"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/5657\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}