{"id":1695,"date":"2022-04-07T21:43:32","date_gmt":"2022-04-07T13:43:32","guid":{"rendered":"https:\/\/zhuxinyong.com\/?p=1695"},"modified":"2023-02-16T23:37:43","modified_gmt":"2023-02-16T15:37:43","slug":"38-asyncawait-chang-jian-de-cuo-wu-2","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=1695","title":{"rendered":"38 &#8211; Async\/Await: \u5e38\u89c1\u7684\u9519\u8bef"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2023\/02\/16493451620596.jpg\" alt=\"\" \/><\/p>\n<p>\u5f53\u6211\u4eec\u8212\u670d\u7684\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 await \u7684\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u8ba4\u8bc6\u5230\u4e00\u4e9b\u6211\u4eec\u4e0d\u80fd\u505a\u7684\uff1a<\/p>\n<ol>\n<li>\u5728\u6ca1\u6709 async \u6807\u6ce8\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 await\uff1b<\/li>\n<li>\u5728\u9876\u5c42\u4f7f\u7528 await\uff1b<\/li>\n<\/ol>\n<p>\u7b2c\u4e00\u4e2a\u6211\u4eec\u5728\u4e4b\u524d\u7684\u6587\u7ae0\u5df2\u7ecf\u8ba8\u8bba\u8fc7\uff0c\u90a3\u7b2c\u4e8c\u4e2a\u6211\u4eec\u63a5\u4e0b\u6765\u4ecb\u7ecd\uff1a<\/p>\n<pre><code class=\"language-plain_text\">async function wait(message, time) {\n    return new Promise((resolve) =&gt; setTimeout(resolve(message), time));\n}\n\nawait wait (&quot;hello&quot;, 2000); \/\/ SyntaxError: await is only allows inside an async function\n<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u91cd\u5199\u4f7f\u5176\u6b63\u5e38\u5de5\u4f5c\uff1a<\/p>\n<pre><code class=\"language-plain_text\">async function wait(message, time) {\n    return new Promise((resolve) =&gt; setTimeout(resolve(message), time));\n}\n\nasync function execute() {\n    const message = await wait (&quot;hello&quot;, 2000);\n    console.log(message); \/\/ hello (after 2000 ms)\n}\n\nexecute();\n<\/code><\/pre>\n<p><strong>\u9876\u5c42\u6267\u884c await \u7684\u63d0\u6848<a href=\"https:\/\/github.com\/tc39\/proposal-top-level-await\" target=\"_blank\" rel=\"noopener\">\u5728\u8fd9<\/a>\u4e14V8\u5df2\u7ecf<a href=\"https:\/\/v8.dev\/features\/top-level-await\" target=\"_blank\" rel=\"noopener\">\u652f\u6301<\/a><\/strong>\u3002<\/p>\n<h3><a id=\"%E6%84%8F%E5%A4%96%E7%9A%84%E4%BD%BF%E4%BB%A3%E7%A0%81%E5%90%8C%E6%AD%A5\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u610f\u5916\u7684\u4f7f\u4ee3\u7801\u540c\u6b65<\/h3>\n<p>async\/await \u6700\u5927\u7684\u4e3a\u9898\u5728\u4e8e await \u5f88\u5bb9\u6613\u88ab\u6ee5\u7528\uff0c\u6211\u4eec\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u7cfb\u7edf\u4ee3\u7801\u5f02\u6b65\u6267\u884c\u4f7f\u5f97\u4ee3\u7801\u66f4\u9ad8\u6548\u3002<\/p>\n<p>\u4e3a\u4e86\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u6765\u4e3e\u4e00\u4e2a promise \u7684\u4f8b\u5b50\uff0c\u7136\u540e\u628a\u5b83\u8f6c\u4e3a async\/await \u63a5\u7740\u6539\u6b63\u5b83\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const sayGreeting = (name, time) =&gt; {\n    return new Promise((resolve) =&gt; {\n        setTimeout(() =&gt; {\n            resolve(`Hello ${name}`);\n        }, time);\n    })\n}\n\nsayGreeting(&quot;Parwinder&quot;, 1000)\n    .then((data) =&gt; {\n        console.log(data); \/\/ &quot;Hello Parwinder&quot; after 1 second\n        return sayGreeting(&quot;Lauren&quot;, 2000);\n    })\n    .then((data) =&gt; {\n        console.log(data); \/\/ &quot;Hello Lauren&quot; after 2 seconds\n        return sayGreeting(&quot;Robert&quot;, 500);\n    })\n    .then((data) =&gt; {\n        console.log(data); \/\/ &quot;Hello Robert&quot; after half a second\n        return sayGreeting(&quot;Eliu&quot;, 2000);\n    })\n    .then((data) =&gt; {\n        console.log(data); \/\/ &quot;Hello Eliu&quot; after 2 seconds\n        return sayGreeting(&quot;George&quot;, 1500);\n    })\n    .then((data) =&gt; {\n        console.log(data); \/\/ &quot;Hello George&quot; after 1.5 seconds\n    })\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u5728\u6307\u5b9a\u65f6\u95f4\u540e\u60f3\u67d0\u4e2a\u4eba\u6253\u62db\u547c\uff0cPromise \u907f\u514d\u4e86 callback \u7684\u56de\u8c03\u5730\u72f1\uff0c\u4f46\u4f9d\u65e7\u5b58\u5728\u4e00\u5c42\u56de\u8c03\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 await \u6765\u91cd\u5199\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const sayGreeting = (name, time) =&gt; {\n    return new Promise((resolve) =&gt; {\n        setTimeout(() =&gt; {\n            resolve(`Hello ${name}`);\n        }, time);\n    })\n}\n\nconst main = async () =&gt; {\n    let a = await sayGreeting(&quot;Parwinder&quot;, 1000);\n    console.log(a); \/\/ &quot;Hello Parwinder&quot; after 1 second\n    a = await sayGreeting(&quot;Lauren&quot;, 2000);\n    console.log(a); \/\/ &quot;Hello Lauren&quot; after 2 seconds\n    a = await sayGreeting(&quot;Robert&quot;, 500);\n    console.log(a); \/\/ &quot;Hello Robert&quot; after half a second\n    a = await sayGreeting(&quot;Eliu&quot;, 2000);\n    console.log(a); \/\/ &quot;Hello Eliu&quot; after 2 seconds\n    a = await sayGreeting(&quot;George&quot;, 1500);\n    console.log(a); \/\/ &quot;Hello George&quot; after 1.5 seconds\n}\n\nmain();\n<\/code><\/pre>\n<p>\u6ca1\u6709\u4e86 then \u7684\u56de\u8c03\u4e14\u66f4\u5bb9\u6613\u9605\u8bfb\uff0c\u76ee\u524d\u4e3a\u6b62\u6211\u4eec\u628a promise \u6539\u4e3a\u4e86 async\/await \u800c\u4e14\u4ee3\u7801\u770b\u4e0a\u53bb\u66f4\u597d\uff0c\u9519\u8bef\u5728\u54ea\u91cc\u5462\uff1f<\/p>\n<p>\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u671f\u671b\u5f02\u6b65\u662f\u5e76\u884c\u6267\u884c\u3002\u6bcf\u6b21\u5728 main \u51fd\u6570\u4e2d\u6dfb\u52a0\u4e00\u4e2a await \u5c31\u4f1a\u963b\u6b62\u4ee3\u7801\u5411\u4e0b\u6267\u884c\u76f4\u5230 promise \u5b8c\u6210\u3002\u6211\u4eec\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a promise \u4e14\u53d6\u5f97\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<p>\u6211\u4eec\u4f7f\u7528 Promise.all \u4e0e async\/await \u4e00\u8d77\u4f7f\u5f97\u4ee3\u7801\u66f4\u9ad8\u6548\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const sayGreeting = (name, time) =&gt; {\n    return new Promise((resolve) =&gt; {\n        setTimeout(() =&gt; {\n            resolve(`Hello ${name}`);\n        }, time);\n    })\n}\n\nconst main = async () =&gt; {\n    const a = sayGreeting(&quot;Parwinder&quot;, 1000);\n    const b = sayGreeting(&quot;Lauren&quot;, 2000);\n    const c = sayGreeting(&quot;Robert&quot;, 500);\n    const d = sayGreeting(&quot;Eliu&quot;, 2000);\n    const e = sayGreeting(&quot;George&quot;, 1500);\n    const [greeting1, greeting2, greeting3, greeting4, greeting5] = await Promise.all([a, b, c, d, e]);\n    \/\/ all promises in promise.all\n    console.log(greeting1, greeting2, greeting3, greeting4, greeting5)\n}\n\nmain();\n<\/code><\/pre>\n<p>\u6211\u4eec\u505a\u4e86\u4ec0\u4e48\uff1a<\/p>\n<ol>\n<li>\u6211\u4eec\u628a promise \u5b58\u5728\u53d8\u91cf\u91cc\uff0c\u800c\u4e0d\u662f\u7b49\u5f85 promise \u5b8c\u6210\uff1b<\/li>\n<li>\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5de8\u5927\u7684 promise \u96c6\u5408\uff1b<\/li>\n<li>\u6211\u4eec await \u8fd9\u4e2a Promise.all \u800c\u4e0d\u662f\u72ec\u7acb\u7684 promise\uff1b<\/li>\n<li>Promise.all \u540c\u65f6\u6267\u884c\u6240\u6709\u7684 promise \uff0c\u5e76\u7b49\u5f85\u6240\u6709\u7ed3\u679c\u8fd4\u56de\u5e76\u8d4b\u503c\u7ed9\u5bf9\u5e94\u7684\u53d8\u91cf\uff1b<\/li>\n<li>\u6253\u5370\u8f93\u51fa\u3002<\/li>\n<\/ol>\n<p>\u5e0c\u671b\u53ef\u4ee5\u63d0\u9ad8\u4f60\u4f7f\u7528 async\/await \u7684\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u6211\u4eec\u8212\u670d\u7684\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 await \u7684\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u8ba4\u8bc6\u5230\u4e00\u4e9b\u6211\u4eec\u4e0d\u80fd\u505a\u7684\uff1a \u5728\u6ca1\u6709 async &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[135],"class_list":["post-1695","post","type-post","status-publish","format-standard","hentry","category-all","tag-js-parwinder"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1695"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1695\/revisions"}],"predecessor-version":[{"id":1696,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/1695\/revisions\/1696"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}