{"id":6330,"date":"2022-07-19T19:03:07","date_gmt":"2022-07-19T11:03:07","guid":{"rendered":"https:\/\/www.lilichao.com\/?p=6330"},"modified":"2022-07-19T20:31:35","modified_gmt":"2022-07-19T12:31:35","slug":"async%e5%92%8cawait","status":"publish","type":"post","link":"https:\/\/lilichao.com\/?p=6330","title":{"rendered":"async\u548cawait"},"content":{"rendered":"\n<p>\u524d\u8fb9\u56de\u7b54\u4e86\u5173\u4e8ePromise\u7684\u95ee\u9898\uff0c\u53c8\u6709\u540c\u5b66\u63d0\u51fa\u4e86\u65b0\u7684\u95ee\u9898\uff1aasync\u548cawait\u662f\u5e72\u4ec0\u4e48\u7528\u7684\uff1f\u8fd9\u8282\u8bfe\u54b1\u4eec\u6765\u89e3\u91ca\u4e00\u4e0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">async<\/h2>\n\n\n\n<p>\u5b66\u4e60\u4e86Promise\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4fbf\u53ef\u4ee5\u50cf\u8fd9\u6837\u521b\u5efa\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>function fn(){\n    return new Promise(resolve =&gt; {\n        resolve(10)\n    })\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u4e0a\u4f8b\u4e2d\uff0c\u51fd\u6570\u529f\u80fd\u6bd4\u8f83\u7b80\u5355\uff0c\u5373\u8c03\u7528\u51fd\u6570\u540e\u8fd4\u56de\u4e86\u4e00\u4e2a\u6570\u5b5710\uff08\u901a\u8fc7Promise\u5f02\u6b65\u8fd4\u56de\uff09\u3002\u4f46\u662f\u4e0d\u7ba1\u8fd4\u56de\u4ec0\u4e48\u6837\u7684\u7ed3\u679c\uff0c\u5f02\u6b65\u51fd\u6570\u672c\u8eab\u7684\u8fd4\u56de\u503c\u5fc5\u987b\u5f97\u662fPromise\u624d\u80fd\u79f0\u5f97\u4e0a\u662f\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\u3002\u4f46\u662f\u5f88\u660e\u663e\uff0c\u5982\u679c\u4f60\u7684\u5f02\u6b65\u51fd\u6570\u53ea\u662f\u8fd4\u56de\u4e00\u4e2a\u5355\u4e2a\u7684\u503c\uff08\u6bd4\u598210\uff09\u5374\u56e0\u6b64\u8981\u7f16\u5199\u521b\u5efaPromise\u7684\u4e00\u5927\u4e32\u4ee3\u7801\u663e\u7136\u662f\u5212\u4e0d\u6765\u7684\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0cJS\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86async\u5173\u952e\u5b57\u3002\u53ea\u9700\u8981\u5728\u521b\u5efa\u51fd\u6570\u65f6\uff0c\u5728function\u524d\u4f7f\u7528async\u5173\u952e\u5b57\uff0c\u5373\u53ef\u4f7f\u4e00\u4e2a\u666e\u901a\u51fd\u6570\u5347\u7ea7\u4e3a\u5f02\u6b65\u51fd\u6570\uff0c\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>async function fn2(){\n    return 10\n}<\/code><\/pre><\/div>\n\n\n\n<p>\u4e0a\u4f8b\u4e2dfn2\u4f7f\u7528\u4e86async\u6765\u58f0\u660e\uff0c\u6b64\u65f6fn2\u53d8\u6210\u4e3a\u4e86\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\u3002\u5f02\u6b65\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u7279\u70b9\u4fbf\u662f\u5b83\u7684\u8fd4\u56de\u503c\u4f1a\u81ea\u52a8\u88ab\u5305\u88c5\u4e3a\u4e00\u4e2aPromise\uff0c\u4e0a\u4f8b\u4e2dfn2\u7684\u8fd4\u56de\u503c\u867d\u7136\u662f10\uff0c\u4f46\u662f\u5982\u679c\u4f60\u5c1d\u8bd5\u53bb\u63a5\u6536\u7684\u8bdd\uff0c\u4f60\u4f1a\u53d1\u73b0\u5b83\u8fd4\u56de\u7684\u662f\u4e00\u4e2aPromise\u3002\u8fd9\u6837\u770b\u6765\uff0cfn1\u548cfn2\u7684\u529f\u80fd\u662f\u4e00\u6a21\u4e00\u6837\u7684\uff0c\u4e0d\u540c\u70b9\u5728\u4e8efn1\u7684Promise\u662f\u6211\u4eec\u624b\u52a8\u521b\u5efa\u7684\uff0c\u800cfn2\u7684\u5374\u662f\u81ea\u52a8\u521b\u5efa\u7684\u3002<\/p>\n\n\n\n<p>\u7b80\u800c\u8a00\u4e4b\uff0casync\u7b80\u5316\u4e86\u5f02\u6b65\u51fd\u6570\u7684\u521b\u5efa\uff0c\u7701\u7565\u4e86\u90e8\u5206Promise\u7684\u521b\u5efa\u5de5\u4f5c\u3002\u4f46\u5982\u679c\u4ec5\u4ec5\u662f\u8fd9\u6837\u90a3async\u7684\u529f\u80fd\u5c31\u663e\u5f97\u5341\u5206\u7684\u9e21\u808b\u4e86\uff0c\u56e0\u4e3a\u8fd9\u79cd\u7b80\u5316\u53ea\u9002\u7528\u4e8e\u5355\u4e2a\u8fd4\u56de\u7ed3\u679c\u7684\u5f02\u6b65\u51fd\u6570\uff0c\u5982\u679c\u51fd\u6570\u529f\u80fd\u6bd4\u8f83\u590d\u6742\u4f9d\u7136\u8fd8\u662f\u9700\u8981\u624b\u52a8\u521b\u5efaPromise\u3002\u6240\u4ee5async\u8fd8\u6709\u7b2c\u4e8c\u4e2a\u7279\u70b9\uff1a\u5728async\u58f0\u660e\u7684\u51fd\u6570\u4e2d\u53ef\u4ee5\u4f7f\u7528await\u5173\u952e\u5b57\u3002\u8fd9\u662f\u600e\u4e48\u56de\u4e8b\uff1f\u4e0d\u6025\uff0c\u4e0b\u4e00\u5c0f\u8282\u6211\u4eec\u4ecb\u7ecd\uff0c\u8fd9\u91cc\u5148\u603b\u7ed3\u4e00\u4e0basync\u7684\u7279\u70b9\uff1a<\/p>\n\n\n\n<ol><li>async\u521b\u5efa\u7684\u51fd\u6570\u5176\u8fd4\u56de\u503c\u4f1a\u81ea\u52a8\u5c01\u88c5\u5230\u4e00\u4e2aPromise\u4e2d\u8fd4\u56de<\/li><li>async\u521b\u5efa\u7684\u51fd\u6570\u4e2d\u53ef\u4ee5\u4f7f\u7528await\u5173\u952e\u5b57<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">await<\/h2>\n\n\n\n<p>\u8c03\u7528\u4e00\u4e2a\u5f02\u6b65\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u5f97\u8fd9\u6837\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>fn2()\n    .then(num =&gt; console.log(num))\n    .catch(err =&gt; console.log(&quot;\u51fa\u9519\u4e86&quot;))<\/code><\/pre><\/div>\n\n\n\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5e76\u4e0d\u4f18\u96c5\uff0c\u4e14\u5f53\u6211\u4eec\u9700\u8981\u540c\u65f6\u8c03\u7528\u591a\u4e2a\u5f02\u6b65\u51fd\u6570\u65f6\uff0c\u6211\u4eec\u5fc5\u987b\u8981\u4e00\u76f4then\u4e0b\u53bb\uff0c\u8fd9\u6837\u5f02\u6b65\u51fd\u6570\u7684\u8c03\u7528\u5c31\u53d8\u5f97\u66f4\u52a0\u7684\u4e0d\u4f18\u96c5\u4e86\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>async function fn2(){\n    return 10\n}\n\nasync function fn3(num){\n    return num + 5\n}\n\nasync function fn4(num){\n    return num + 10\n}\n\nasync function fn5(num){\n    return num + 20\n}\n\nfn2()\n    .then(fn3)\n    .then(fn4)\n    .then(fn5)\n    .then(console.log)\n    .catch(err =&gt; console.log(&quot;\u51fa\u9519\u4e86&quot;))<\/code><\/pre><\/div>\n\n\n\n<p>\u5f53\u4f60\u5f02\u6b65\u7528\u5f97\u4e45\u4e86\uff0c\u4f60\u4f1a\u53d1\u73b0\u4f60\u975e\u5e38\u7684\u6000\u5ff5\u4e4b\u524d\u540c\u6b65\u4ee3\u7801\u7684\u7f16\u5199\u65b9\u5f0f\uff0c\u4e00\u884c\u4e00\u884c\u7684\uff0c\u987a\u5e8f\u7279\u522b\u7684\u6e05\u6670\uff0c\u903b\u8f91\u4e5f\u6bd4\u8f83\u5bb9\u6613\u7406\u89e3\uff0c\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>let result = fn2()\nresult = fn3(result)\nresult = fn4(result)\nresult = fn5(result)\nconsole.log(result)<\/code><\/pre><\/div>\n\n\n\n<p>\u4f46\u662f\u4e0d\u884c\u554a\uff0c\u4e0a\u8fb9\u7684\u51e0\u4e2a\u51fd\u6570\u90fd\u662f\u5f02\u6b65\u51fd\u6570\uff0c\u8fd4\u56de\u503c\u90fd\u662fPromise\uff0c\u6211\u8fd9\u4e48\u5199\u7b49\u4e8e\u662f\u5c06Promise\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\uff0c\u867d\u7136\u80fd\u591f\u6267\u884c\uff0c\u4f46\u662f\u7ed3\u679c\u80af\u5b9a\u4e0d\u5bf9\u554a\uff01\u563f\u563f\uff0c\u4e3a\u4e86\u89e3\u51b3\u6211\u4eec\u8fd9\u4e9b\u65e2\u559c\u6b22\u540c\u6b65\u4ee3\u7801\u7684\u7f16\u5199\u65b9\u5f0f\uff0c\u53c8\u60f3\u8981\u4f7f\u7528\u5f02\u6b65\u7684\u4eba\uff0cJS\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2aawait\u5173\u952e\u5b57\uff0c\u8ba9\u6211\u4eec\u7684\u7406\u60f3\u7167\u8fdb\u4e86\u73b0\u5b9e\u3002<\/p>\n\n\n\n<p>\u8c03\u7528\u5f02\u6b65\u51fd\u6570\u65f6\u9664\u4e86\u53ef\u4ee5\u901a\u8fc7then\u65b9\u6cd5\u6765\u8bfb\u53d6\u7ed3\u679c\u4ee5\u5916\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728\u8c03\u7528\u65f6\u4f7f\u7528await\u5173\u952e\u5b57\uff0c\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>let result = await fn2()<\/code><\/pre><\/div>\n\n\n\n<p>\u8fd9\u6837\u8c03\u7528\u540e\uff0c\u5f02\u6b65\u51fd\u6570\u4f1a\u5728\u6709\u6267\u884c\u7ed3\u679c\u540e\uff0c\u5c06\u6267\u884c\u7ed3\u679c\u8fd4\u56de\uff08\u5982\u679c\u6709\u9519\u8bef\u5c31\u81ea\u52a8\u62a5\u9519\uff09\u3002\u4f46\u662f\uff0c\u4f60\u5343\u4e07\u4e0d\u8981\u7740\u6025\u53bb\u5c1d\u8bd5\u8fd9\u884c\u4ee3\u7801\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u5f0f\u4e00\u773c\u770b\u4e0a\u53bb\u5c31\u4e0d\u5bf9\uff01fn2\u662f\u5f02\u6b65\u7684\uff0c\u5b83\u4e0d\u4f1a\u7acb\u523b\u8fd4\u56de\u7ed3\u679c\uff0c\u6b63\u5e38\u6765\u8bb2\u5b83\u4f1a\u5728\u5176\u4ed6\u4ee3\u7801\uff08\u5b83\u4e0b\u8fb9\u7684\u4ee3\u7801\uff09\u6267\u884c\u5b8c\u624d\u4f1a\u8fd4\u56de\u7ed3\u679c\uff0c\u6240\u4ee5\u5b83\u5e94\u8be5\u8fd4\u56de\u7684\u662f\u4e00\u4e2aPromise\u800c\u4e0d\u5e94\u8be5\u662f\u4e00\u4e2a\u6709\u6548\u503c\u3002\u5982\u679c\u5b83\u771f\u7684\u8fd4\u56de\u4e86\u4e00\u4e2a\u6709\u6548\u7684\u503c\uff0c\u8fd9\u4e5f\u5c31\u610f\u5473\u7740\uff0c\u5b83\u8fd4\u56de\u7ed3\u679c\u524d\uff0c\u5176\u540e\u7684\u4ee3\u7801\u662f\u4e0d\u80fd\u6267\u884c\u7684\uff0c\u90a3\u4e48\u8fd9\u4e5f\u5c31\u5931\u53bb\u4e86\u5f02\u6b65\u672c\u8eab\u7684\u610f\u4e49\u4e86\uff08\u4f1a\u963b\u585e\u5176\u4ed6\u4ee3\u7801\u7684\u6267\u884c\uff09\uff01<\/p>\n\n\n\n<p>\u6240\u4ee5\u6ce8\u610f\u4e86await\u5e76\u4e0d\u80fd\u4efb\u610f\u7684\u4f7f\u7528\uff0c\u56e0\u4e3aawait\u5e76\u4e0d\u4f1a\u6539\u53d8\u5f02\u6b65\u7684\u672c\u8d28\uff0c\u5b83\u53ea\u662f\u6539\u53d8\u4e86\u5f02\u6b65\u7684\u8c03\u7528\u65b9\u5f0f\uff0c\u8ba9\u6211\u4eec\u50cf\u7f16\u5199\u540c\u6b65\u4ee3\u7801\u4e00\u6837\u53bb\u4f7f\u7528\u5f02\u6b65\u3002\u800c\u4e0d\u662f\u5c06\u5f02\u6b65\u6539\u56de\u5230\u540c\u6b65\uff08\u771f\u662f\u8fd9\u6837\u7684\u8bdd\u5c31\u5f97\u4e0d\u507f\u5931\u4e86\uff09\u3002\u90a3\u4e48\u5728\u54ea\u91cc\u80fd\u591f\u4f7f\u7528await\u5462\uff1f\u6709\u4e24\u4e2a\u5730\u65b9\uff1a<\/p>\n\n\n\n<ol><li>async\u5173\u952e\u5b57\u521b\u5efa\u7684\u51fd\u6570<\/li><li>JS\u6a21\u5757\u7684\u6700\u5916\u5c42\u4f5c\u7528\u57df<\/li><\/ol>\n\n\n\n<p>\u50cf\u8fd9\u6837\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>async function fn6(){\n    try{\n        let result = await fn2()\n        result = await fn3(result)\n        result = await fn4(result)\n        result = await fn5(result)\n        console.log(result)\n    }catch(e){\n        console.log(&quot;\u51fa\u9519\u4e86&quot;)\n    }\n    \n}\n\nfn6()<\/code><\/pre><\/div>\n\n\n\n<p>\u6216\u8005\u662f\u533f\u540d\u7684\u5f02\u6b65\u51fd\u6570\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-js\" data-lang=\"JavaScript\"><code>(async ()=&gt;{\n    try{\n        let result = await fn2()\n        result = await fn3(result)\n        result = await fn4(result)\n        result = await fn5(result)\n        console.log(result)\n    }catch(e){\n        console.log(&quot;\u51fa\u9519\u4e86&quot;)\n    }\n})()<\/code><\/pre><\/div>\n\n\n\n<p>\u6216\u8005\u662fJS\u6a21\u5757\u4e2d\uff1a<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;script type=&quot;module&quot;&gt;    \n    try{\n        let result = await fn2()\n        result = await fn3(result)\n        result = await fn4(result)\n        result = await fn5(result)\n        console.log(result)\n    }catch(e){\n        console.log(&quot;\u51fa\u9519\u4e86&quot;)\n    }\n&lt;\/script&gt;<\/code><\/pre><\/div>\n\n\n\n<p>await\u7684\u6267\u884c\u539f\u7406\u5e76\u4e0d\u96be\u7406\u89e3\uff0c\u56e0\u4e3aawait\u5fc5\u987b\u5199\u5728\u5f02\u6b65\u51fd\u6570\u4e2d\u6216\u6a21\u5757\u4e2d\uff0c\u800c\u5f02\u6b65\u51fd\u6570\u548c\u6a21\u5757\u672c\u8eab\u5c31\u662f\u5f02\u6b65\u7684\uff0c\u6240\u4ee5\u5f53\u6267\u884c\u5230await\u7684\u4ee3\u7801\u65f6\uff0c\u5f02\u6b65\u51fd\u6570\uff08\u6216\u6a21\u5757\uff09\u4f1a\u6682\u505c\u6267\u884c\uff0c\u7b49\u5230await\u6807\u8bc6\u7684\u5f02\u6b65\u4ee3\u7801\u6709\u7ed3\u679c\u65f6\u5728\u7ee7\u7eed\u6267\u884c\u51fd\u6570\uff08\u6216\u6a21\u5757\uff09\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u8c03\u7528\u65b9\u5f0f\u7684\u6539\u53d8\uff0c\u9519\u8bef\u7684\u5904\u7406\u65b9\u5f0f\u4e5f\u4e0d\u518d\u662fcatch\u65b9\u6cd5\uff0c\u800c\u662f\u5728\u540c\u6b65\u8c03\u7528\u4e2d\u6240\u4f7f\u7528\u7684try-catch\u3002<\/p>\n\n\n\n<p>\u5b66\u4e60\u5b8c\u4e86await\u6211\u4eec\u4e5f\u5c31\u7406\u89e3\u4e86\uff0casync\u7684\u7b2c\u4e8c\u4e2a\u4f5c\u7528\uff0c\u76f8\u8f83\u4e8e\u7b80\u5316\u5f02\u6b65\u51fd\u6570\u7684\u521b\u5efa\u80fd\u591f\u4f7f\u7528await\u624d\u662fasync\u51fd\u6570\u7684\u6700\u5927\u4f5c\u7528\u3002\u5f53\u7136\u65e0\u8bba\u662fawait\u8c03\u7528\u8fd8\u662f\u76f4\u63a5\u8c03\u7528\uff0c\u5e76\u6ca1\u6709\u672c\u8d28\u533a\u522b\uff0c\u53ea\u662f\u8c03\u7528\u65b9\u5f0f\u7684\u4e0d\u540c\uff0c\u6240\u4ee5\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\uff0c\u770b\u4f60\u81ea\u5df1\uff01<\/p>\n\n\n\n<p>\u4e0b\u8f7d\u5730\u5740\uff1a<\/p>\n\n\n\n<p>\u94fe\u63a5\uff1a<a href=\"https:\/\/pan.baidu.com\/s\/14Cqjzx4I_TMjeFf4GBIkBg?pwd=yvft\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/pan.baidu.com\/s\/14Cqjzx4I_TMjeFf4GBIkBg?pwd=yvft<\/a><br>\u63d0\u53d6\u7801\uff1ayvft<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8fb9\u56de\u7b54\u4e86\u5173\u4e8ePromise\u7684\u95ee\u9898\uff0c\u53c8\u6709\u540c\u5b66\u63d0\u51fa\u4e86\u65b0\u7684\u95ee\u9898\uff1aasync\u548cawait\u662f\u5e72\u4ec0\u4e48\u7528\u7684\uff1f\u8fd9\u8282\u8bfe\u54b1\u4eec\u6765\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":6283,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[271],"tags":[],"_links":{"self":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/6330"}],"collection":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6330"}],"version-history":[{"count":4,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/6330\/revisions"}],"predecessor-version":[{"id":6334,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/6330\/revisions\/6334"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/media\/6283"}],"wp:attachment":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}