{"id":968,"date":"2022-04-04T23:10:10","date_gmt":"2022-04-04T15:10:10","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=968"},"modified":"2022-04-05T22:41:25","modified_gmt":"2022-04-05T14:41:25","slug":"39-asyncawait-cuo-wu-chu-li","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=968","title":{"rendered":"39 &#8211; Async\/Await:\u9519\u8bef\u5904\u7406"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2022\/04\/16491696350162.jpg\" alt=\"\" \/><\/p>\n<p>Promises \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 then \u4e2d\u7684\u9519\u8bef\u5904\u7406\u6a21\u5757\u6216\u8005 catch \u6a21\u5757\u6765\u5904\u7406\u9519\u8bef\u3002Async\/await \u6709\u76f8\u8fd1\u7684\u7b56\u7565\u3002<\/p>\n<h2><a id=\"await%E4%B8%8E-catch%E4%B8%80%E8%B5%B7%E4%BD%BF%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>await \u4e0e catch \u4e00\u8d77\u4f7f\u7528<\/h2>\n<p>\u6211\u4eec\u4f7f\u7528 await \u4e00\u4e2a\u5f02\u6b65\u7684\u51fd\u6570\uff0c\u901a\u5e38\u60c5\u51b5\u4e0b await \u64cd\u4f5c\u7b26\u662f\u4e0e Promise \u4e00\u8d77\u4f7f\u7528\u7684\uff0c\u800c\u4e14\u53ea\u80fd\u518d async \u51fd\u6570\u5185\u90e8\u3002Async \u51fd\u6570\u63d0\u4f9b\u4e86 promises \u6240\u4ee5\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528 catch \u5757\u6765\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"language-plain_text\">const myPromise = async () =&gt; {\n    return new Promise((resolve, reject) =&gt; {\n        setTimeout(() =&gt; {\n            reject(&quot;We ran into an error&quot;);\n        }, 2000);\n    });\n}\n\nconst main = async () =&gt; {\n    const response = await myPromise().catch((err) =&gt; {\n        console.log(err); \/\/ We ran into an error\n    })\n}\n\nmain();\n<\/code><\/pre>\n<p>myPromise 2\u79d2\u949f\u540e reject \u5e76\u6253\u5370\u51fa\u4e00\u6761\u4fe1\u606f\uff0c\u5f53\u6211\u4eec\u5728\u7b49\u5f85 promise \u7ed3\u679c\u7684\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5176\u540e\u589e\u52a0 catch \u6765\u5904\u7406\u9519\u8bef\u3002<\/p>\n<h3><a id=\"%E5%BD%93%E8%B0%83%E7%94%A8async%E5%87%BD%E6%95%B0%E6%97%B6%EF%BC%8C%E4%BD%BF%E7%94%A8-catch\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5f53\u8c03\u7528 async \u51fd\u6570\u65f6\uff0c\u4f7f\u7528 catch<\/h3>\n<p>\u5f53\u6211\u4eec\u8c03\u7528 async \u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u5728\u5176\u540e\u6dfb\u52a0 catch\u3002<\/p>\n<pre><code class=\"language-plain_text\">const myPromise = async () =&gt; {\n    return new Promise((resolve, reject) =&gt; {\n        setTimeout(() =&gt; {\n            reject(&quot;We ran into an error&quot;);\n        }, 2000);\n    });\n}\n\nconst main = async () =&gt; {\n    const response = await myPromise();\n}\n\nmain().catch((err) =&gt; {\n    console.log(err); \/\/ We ran into an error\n});\n<\/code><\/pre>\n<p>\u56e0\u4e3a main \u662f\u4e00\u4e2a async \u51fd\u6570\u4e14\u8fd4\u56de\u4e00\u4e2a promise \uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 catch \u8fde\u4e00\u8d77\u3002\u4f7f\u7528 catch \u4e0d\u9519\u4f46\u6709\u4e00\u4e2a\u7f3a\u70b9\uff0c\u5b83\u4f1a\u6355\u83b7\u6240\u6709\u9519\u8bef\u800c\u4e0d\u5355\u5355\u662f myPromise \u5f15\u8d77\u7684\u3002<\/p>\n<h3><a id=\"%E4%BD%BF%E7%94%A8%E9%AB%98%E9%98%B6%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u9ad8\u9636\u51fd\u6570<\/h3>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 catch \u6765\u6355\u83b7\u9519\u8bef\uff0c\u4f46\u5047\u8bbe\u4f60\u9879\u76ee\u4e2d\u6709\u8bb8\u591a async \u51fd\u6570\uff0c\u4e3a\u6bcf\u4e00\u4e2a\u6dfb\u52a0 catch \u4f1a\u4ee4\u4eba\u538c\u70e6\u4f46\u4f60\u6709\u4e0d\u5f97\u4e0d\u52a0\uff0c\u8be5\u600e\u4e48\u529e\uff1f<\/p>\n<p>\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\uff0c\u5b83\u5c31\u662f\u628a\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u8f93\u5165\u7136\u540e\u8f93\u51fa\u53e6\u4e00\u4e2a\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-plain_text\">const myPromise = async () =&gt; {\n    return new Promise((resolve, reject) =&gt; {\n        setTimeout(() =&gt; {\n            reject(&quot;We ran into an error&quot;);\n        }, 2000);\n    });\n}\n\nconst main = async () =&gt; {\n    const response = await myPromise();\n}\n\nconst handleError = (err) =&gt; {\n    console.log(err); \/\/ We ran into an error\n}\n\nconst addingErrorHandler = (fn, errorHandler) =&gt; {\n    return function() {\n        fn().catch(errorHandler);\n    }\n}\n\nconst mainFunctionWithErrorHandler = addingErrorHandler(main, handleError);\n\nmainFunctionWithErrorHandler();\n<\/code><\/pre>\n<p>\u6211\u4eec\u6dfb\u52a0\u4e86\u4e09\u4e2a\u65b0\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li><strong>handleError<\/strong> \u4e3a\u4e00\u4e2a\u6807\u51c6\u7684\u9519\u8bef\u5904\u7406\u51fd\u6570\u3002<\/li>\n<li><strong>addingErrorHandler<\/strong> \u4e3a\u4e00\u4e2a\u9ad8\u9636\u51fd\u6570\uff0c\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u5165\u53c2\u540c\u65f6\u4e3a\u8be5\u51fd\u6570\u6dfb\u52a0\u9519\u8bef\u5904\u7406\u3002<\/li>\n<li><strong>mainFunctionWithErrorHandler<\/strong> \u4e3a main \u51fd\u6570\u7ecf\u8fc7\u9ad8\u9636\u51fd\u6570\u5904\u7406\u540e\u80fd\u5904\u7406\u5f02\u5e38\u7684\u51fd\u6570\u3002<\/li>\n<\/ol>\n<p>\u56e0\u4e3a\u4f8b\u5b50\u4ec5\u4ec5\u5904\u7406\u4e86\u4e00\u4e2a main \u51fd\u6570\uff0c\u4f60\u53ef\u80fd\u4f1a\u611f\u89c9\u6dfb\u52a0\u592a\u591a\u7684\u51fd\u6570\u3002\u4f46\u6211\u4eec\u53ef\u4ee5\u5728\u5927\u9879\u76ee\u4e2d\u901a\u8fc7\u9ad8\u9636\u51fd\u6570\u5904\u7406\u4e0d\u8ba1\u5176\u6570\u7684 async \u51fd\u6570\u3002<\/p>\n<h3><a id=\"%E4%BD%BF%E7%94%A8trycatch%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528 try\/catch \u51fd\u6570<\/h3>\n<p>JavaScript \u4e3a\u6211\u4eec\u6dfb\u52a0\u4e86 try\/catch \u5feb\u6765\u5904\u7406\u5f02\u5e38\uff0c\u540c\u6837\u53ef\u4ee5\u5e94\u7528\u5728 async\/await \u4e0a\uff1a<\/p>\n<pre><code class=\"language-plain_text\">const myPromise = async () =&gt; {\n    return new Promise((resolve, reject) =&gt; {\n        setTimeout(() =&gt; {\n            reject(&quot;We ran into an error&quot;);\n        }, 2000);\n    });\n}\n\nconst main = async () =&gt; {\n    try {\n        await myPromise(); \/\/ try this code\n    } catch (err) { \/\/ if it throws an error execute the catch block\n        console.log(err); \/\/ We ran into an error\n    }\n}\n\nmain();\n<\/code><\/pre>\n<p>try\/catch \u662f\u6700\u7b80\u5355\u6613\u61c2\u7684\u65b9\u5f0f\uff0c\u4f46\u5728\u5927\u578b\u9879\u76ee\u4e2d\u6211\u8fd8\u662f\u559c\u6b22\u4f7f\u7528\u9ad8\u9636\u51fd\u6570\u6765\u5904\u7406 async\/await \u7684\u5f02\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Promises \u5141\u8bb8\u6211\u4eec\u901a\u8fc7 then \u4e2d\u7684\u9519\u8bef\u5904\u7406\u6a21\u5757\u6216\u8005 catch \u6a21\u5757\u6765\u5904\u7406\u9519\u8bef\u3002Asyn&#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,20,3],"tags":[84,6,85],"class_list":["post-968","post","type-post","status-publish","format-standard","hentry","category-all","category-frontend","category-tech","tag-async-await","tag-javascript","tag-try-catch"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/968","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=968"}],"version-history":[{"count":1,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/968\/revisions"}],"predecessor-version":[{"id":969,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/968\/revisions\/969"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=968"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=968"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=968"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}