{"id":13230,"date":"2023-11-29T10:49:34","date_gmt":"2023-11-29T02:49:34","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=13230"},"modified":"2023-11-29T10:49:39","modified_gmt":"2023-11-29T02:49:39","slug":"qian-duan-mian-shi-ti-shen-me-shiasyncawait-ji-qi","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/29\/qian-duan-mian-shi-ti-shen-me-shiasyncawait-ji-qi\/","title":{"rendered":"\u524d\u7aef\u9762\u8bd5\u9898\uff1a\u4ec0\u4e48\u662fasync\/await\u53ca\u5176\u5982\u4f55\u5de5\u4f5c\uff1f"},"content":{"rendered":"<p>async\/await\u662fJS\u4e2d\u7f16\u5199\u5f02\u6b65\u6216\u975e\u963b\u585e\u4ee3\u7801\u7684\u65b0\u65b9\u6cd5\u3002\u5b83\u5efa\u7acb\u5728Promises\u4e4b\u4e0a\uff0c\u8ba9\u5f02\u6b65\u4ee3 \u7801\u7684\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u5ea6\u90fd\u66f4\u9ad9\u3002<br \/>\nasync\/await\u662fJS\u4e2d\u7f16\u5199\u5f02\u6b65\u6216\u975e\u963b\u585e\u4ee3\u7801\u7684\u65b0\u65b9\u6cd5\u3002\u5b83\u5efa\u7acb\u5728Promises\u4e4b\u4e0a\uff0c\u76f8\u5bf9\u4e8e Promise\u548c\u56de\u8c03\uff0c\u5b83\u7684\u53ef\u8bfb\u6027\u548c\u7b80\u6d01\u5ea6\u90fd\u66f4\u9ad8\u3002\u4f46\u662f\uff0c\u5728\u4f7f\u7528\u6b64\u529f\u80fd\u4e4b\u524d\uff0c\u6211\u4eec\u5fc5\u987b\u5148\u5b66\u4e60 Promises\u7684\u57fa\u7840\u77e5\u8bc6\uff0c\u56e0\u4e3a\u6b63\u5982\u6211\u4e4b\u524d\u6240\u8bf4\uff0c\u5b83\u662f\u57fa\u4e8ePromise\u6784\u5efa\u7684\uff0c\u8fd9\u610f\u5473\u7740\u5e55\u540e\u4f7f\u7528 \u4ecd\u7136\u662fPromise\u3002<br \/>\n\u4f7f\u7528 Promise<br \/>\nfunction callApi() {<br \/>\nreturn fetch(&quot;url\/to\/api\/endpoint&quot;)<br \/>\n.then(resp =&gt; resp. json())<br \/>\n.then(data =&gt; {<br \/>\n\/\/do something with &quot;data&quot;<br \/>\n}). catch(err =&gt; {<br \/>\n\/\/do something with &quot;err&quot;<br \/>\n});<br \/>\n}<br \/>\n\u4f7f\u7528 async\/await<br \/>\n\u5728async\/await,\u6211\u4eec\u4f7f\u7528tru\/catch\u8bed\u6cd5\u6765\u6355\u83b7\u5f02\u5e38\u3002<br \/>\nasync function callApi() {<br \/>\ntry {<br \/>\nconst resp = await fetch(&quot;url\/to\/api\/endpoint&quot;)\uff1b<br \/>\nconst data = await resp.json()\uff1b<br \/>\n\/\/do something with &quot;data&quot;<br \/>\n} catch (e) {<br \/>\n\/\/do something with &quot;err&quot;<br \/>\n}<br \/>\n}<br \/>\n\u6ce8\u610f\uff1a\u4f7f\u7528async\u5173\u952e\u58f0\u660e\u51fd\u6570\u4f1a\u9690\u5f0f\u8fd4\u56de\u4e00\u4e2aPromiseo<br \/>\nconst giveMeOne = async () =&gt; 1\uff1b giveMeOne()<br \/>\n.then((num) =&gt; {<br \/>\nconsole. log(num)\uff1b \/\/ logs 1<br \/>\n});<br \/>\n\u6ce8\u610f:await\u5173\u952e\u5b57\u53ea\u80fd\u5728async function\u4e2d\u4f7f\u7528\u3002\u5728\u4efb\u4f55\u975easync function\u7684\u51fd\u6570\u4e2d\u4f7f\u7528 await\u5173\u952e\u5b57\u90fd\u4f1a\u629b\u51fa\u9519\u8bef\u3002await\u5173\u952e\u5b57\u5728\u6267\u884c\u4e0b\u4e00\u884c\u4ee3\u7801\u4e4b\u524d\u7b49\u5f85\u53f3\u4fa7\u8868\u8fbe\u5f0f(\u53ef\u80fd\u662f\u4e00\u4e2a Promise)\u8fd4\u56de\u3002<br \/>\nconst giveMeOne = async () =&gt; 1\uff1b<br \/>\nfunction getOne() {<br \/>\ntry {<br \/>\nconst num = await giveMeOne()\uff1b<br \/>\nconsole. log(num)\uff1b<br \/>\n} catch (e) {<br \/>\nconsole. log(e)\uff1b<br \/>\n}<br \/>\n\/\/ Uncaught SyntaxError\uff1a await is only valid in async function<br \/>\nasync function getTwo() {<br \/>\ntry {<br \/>\nconst numl = await giveMeOne() \uff1b \/\/\u8fd9\u884c\u4f1a\u7b49\u5f85\u53f3\u4fa7\u8868\u8fbe\u5f0f\u6267\u884c\u5b8c\u6210<br \/>\nconst num2 = await giveMeOne() \uff1b<br \/>\nreturn numl + num2\uff1b<br \/>\n} catch (e) {<br \/>\nconsole. log(e);<br \/>\n}<br \/>\n)<br \/>\nawai.t getTwo(); \/\/ 2<\/p>\n","protected":false},"excerpt":{"rendered":"<p>async\/await\u662fJS\u4e2d\u7f16\u5199\u5f02\u6b65\u6216\u975e\u963b\u585e\u4ee3\u7801\u7684\u65b0\u65b9\u6cd5\u3002\u5b83\u5efa\u7acb\u5728Promises\u4e4b\u4e0a\uff0c\u8ba9\u5f02\u6b65\u4ee3 \u7801\u7684\u53ef\u8bfb\u6027 [&hellip;]<\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103],"tags":[],"class_list":["post-13230","post","type-post","status-publish","format-standard","hentry","category-qianduan"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/13230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=13230"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/13230\/revisions"}],"predecessor-version":[{"id":13231,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/13230\/revisions\/13231"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=13230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=13230"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=13230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}