{"id":52407,"date":"2024-12-03T14:53:04","date_gmt":"2024-12-03T06:53:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52407\/"},"modified":"2024-12-03T14:53:04","modified_gmt":"2024-12-03T06:53:04","slug":"async-await%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bc%98%e9%9b%85%e5%9c%b0%e9%80%80%e5%87%ba%e4%b8%8d%e7%a1%ae%e5%ae%9a%e6%97%b6%e9%97%b4%e5%9b%9e%e8%b0%83%e5%87%bd%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52407\/","title":{"rendered":"Async\/Await\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Async\/Await\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aAsync\/Await\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\uff1f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732087648673d8f606aa78.jpg\" class=\"aligncenter\" title=\"Async\/Await\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\uff1f\u63d2\u56fe\" alt=\"Async\/Await\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p>\u5728async await\u4e2d\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570<\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528async await\u65f6\uff0c\u5982\u4f55\u5728\u76d1\u542c\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\u4e2d\u7684\u4e8b\u4ef6\u65f6\u9000\u51fa\uff1f<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>const a = (): promise&lt;void&gt; =&gt; {\n  \/\/ \u76d1\u542c\u6309\u94ae\u7684\u5c5e\u6027\u53d8\u5316\n  return new promise((resolve) =&gt; {\n    const callback = (mutations: mutationrecord[]) =&gt; {\n      \/\/ \u9000\u51fa\n      resolve();\n    };\n    const observer = new mutationobserver(callback);\n    observer.observe(buttonel, {\n      attributes: true,\n    });\n\n    \/\/ \u89e6\u53d1\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5e76\u76d1\u542c\u5c5e\u6027\u53d8\u5316\n    p().then(() =&gt; {\n      buttonel.click();\n    });\n  });\n};\n\nconst b = async (): promise&lt;void&gt; =&gt; {\n  \/\/ \u76d1\u542c\u6309\u94ae\u7684\u5c5e\u6027\u53d8\u5316\uff0c\u5982\u4f55\u9000\u51fa\uff1f\n  const callback = (mutations: mutationrecord[]) =&gt; {};\n  const observer = new mutationobserver(callback);\n  observer.observe(buttonel, {\n    attributes: true,\n  });\n\n  \/\/ \u89e6\u53d1\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5e76\u76d1\u542c\u5c5e\u6027\u53d8\u5316\n  await p();\n  buttonel.click();\n};<\/pre>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528\u6700\u65b0\u7684promise.withresolvers()\u63a5\u53e3\uff0c\u76ee\u524d\u5904\u4e8estage-4\u9636\u6bb5\u3002<\/p>\n<pre>const b = async (): Promise&lt;void&gt; =&gt; {\n  \/\/ \u521b\u5efa Promise \u5b9e\u4f8b\n  const { promise, resolve } = Promise.withResolvers&lt;void&gt;();\n\n  \/\/ \u8bbe\u7f6e\u56de\u8c03\u548c\u89c2\u5bdf\u5668\n  const callback = (mutations: MutationRecord[]) =&gt; {\n    resolve();\n  };\n  const observer = new MutationObserver(callback);\n  observer.observe(buttonEl, {\n    attributes: true,\n  });\n\n  \/\/ \u89e6\u53d1\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5e76\u76d1\u542c\u5c5e\u6027\u53d8\u5316\n  await p();\n  buttonEl.click();\n\n  \/\/ \u4f7f\u7528 await promise \u6216 return promise \u8fd4\u56de Promise\n  await promise;\n};<\/pre>\n<p>\u8003\u8651\u517c\u5bb9\u6027\uff0c\u53ef\u80fd\u9700\u8981\u66f4\u65b0core-js\u548ctypescript\u7248\u672c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aAsync\/Await\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Head First Java\u4e2d\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u53d6\u6b3e\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u51fa\u73b0\u4f59\u989d\u4e0d\u8db3\u7684\u95ee\u9898\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Async\/Await\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\uff1f \u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60\u6587\u7ae0\uff0c\u90a3\u4e48\u672c\u6587\u300aAsync\/Await\u4e2d\u5982\u4f55\u4f18\u96c5\u5730\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~ \u5728async await\u4e2d\u9000\u51fa\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570 \u95ee\u9898\uff1a \u4f7f\u7528async await\u65f6\uff0c\u5982\u4f55\u5728\u76d1\u542c\u4e0d\u786e\u5b9a\u65f6\u95f4\u56de\u8c03\u51fd\u6570\u4e2d\u7684\u4e8b\u4ef6\u65f6\u9000\u51fa\uff1f \u793a\u4f8b\u4ee3\u7801\uff1a const a = (): promise&lt;void&gt; =&gt; { \/\/ \u76d1\u542c\u6309\u94ae\u7684\u5c5e\u6027\u53d8\u5316 return new promise((resolve) =&gt; { const callback = (mutations: mutationrecord[]) =&gt; { \/\/ \u9000\u51fa resolve(); }; const observer = new mutationobserver(callback); observer.observe(buttonel, { attributes: true, }); \/\/ \u89e6\u53d1\u6309\u94ae\u70b9\u51fb\u4e8b\u4ef6\u5e76\u76d1\u542c\u5c5e\u6027\u53d8\u5316 p().then(() =&gt; { buttonel.click(); }); }); }; const b = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52407","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=52407"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52407\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}