{"id":11920,"date":"2023-11-28T22:37:44","date_gmt":"2023-11-28T14:37:44","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11920"},"modified":"2023-11-30T18:22:16","modified_gmt":"2023-11-30T10:22:16","slug":"javascript-zhong-shi-xian-yi-bu-de-fang-shi-you-na","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/javascript-zhong-shi-xian-yi-bu-de-fang-shi-you-na\/","title":{"rendered":"JavaScript \u4e2d\u5b9e\u73b0\u5f02\u6b65\u7684\u65b9\u5f0f\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p>JavaScript \u4e2d\u5b9e\u73b0\u5f02\u6b65\u64cd\u4f5c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u4e00\u4e9b\u4e3b\u8981\u7684\u65b9\u5f0f\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u56de\u8c03\u51fd\u6570\uff08Callbacks\uff09\uff1a<\/strong> \u901a\u8fc7\u5c06\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\uff0c\u5728\u5f02\u6b65\u64cd\u4f5c\u5b8c\u6210\u540e\u8c03\u7528\u8be5\u51fd\u6570\u3002\u7ecf\u5178\u7684\u56de\u8c03\u5730\u72f1\u95ee\u9898\u662f\u56de\u8c03\u51fd\u6570\u7684\u5d4c\u5957\u8fc7\u6df1\uff0c\u4f7f\u4ee3\u7801\u96be\u4ee5\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<\/li>\n<li>\n<p><strong>Promise\uff1a<\/strong> Promise \u662f\u4e00\u79cd\u7528\u4e8e\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u7684\u5bf9\u8c61\uff0c\u4ee3\u8868\u4e86\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u6700\u7ec8\u5b8c\u6210\u6216\u5931\u8d25\u3002\u5b83\u53ef\u4ee5\u94fe\u5f0f\u5730\u5904\u7406\u591a\u4e2a\u5f02\u6b65\u64cd\u4f5c\uff0c\u5e76\u4e14\u66f4\u5bb9\u6613\u8fdb\u884c\u9519\u8bef\u5904\u7406\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u793a\u4f8b\uff1a\u4f7f\u7528 Promise\nconst fetchData = new Promise((resolve, reject) =&gt; {\n  \/\/ \u5f02\u6b65\u64cd\u4f5c\n  if (\/* \u5f02\u6b65\u64cd\u4f5c\u6210\u529f *\/) {\n    resolve(result); \/\/ \u6210\u529f\u65f6\u8c03\u7528 resolve\n  } else {\n    reject(error); \/\/ \u5931\u8d25\u65f6\u8c03\u7528 reject\n  }\n});\n\nfetchData\n  .then(result =&gt; {\n    \/\/ \u5904\u7406\u6210\u529f\u7684\u60c5\u51b5\n  })\n  .catch(error =&gt; {\n    \/\/ \u5904\u7406\u5931\u8d25\u7684\u60c5\u51b5\n  });\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Async\/Await\uff1a<\/strong> Async\/Await \u662f\u57fa\u4e8e Promise \u7684\u8bed\u6cd5\u7cd6\uff0c\u4f7f\u5f97\u5f02\u6b65\u4ee3\u7801\u770b\u8d77\u6765\u66f4\u50cf\u540c\u6b65\u4ee3\u7801\u3002<code>async<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\u662f\u5f02\u6b65\u7684\uff0c<code>await<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u7b49\u5f85\u4e00\u4e2a\u5f02\u6b65\u64cd\u4f5c\u7684\u7ed3\u679c\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/ \u793a\u4f8b\uff1a\u4f7f\u7528 Async\/Await\nasync function fetchData() {\n  try {\n    const result = await fetch('https:\/\/api.example.com\/data');\n    \/\/ \u5904\u7406\u83b7\u53d6\u7684\u6570\u636e\n  } catch (error) {\n    \/\/ \u5904\u7406\u9519\u8bef\n  }\n}\n\nfetchData();\n<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Generator \u51fd\u6570\uff1a<\/strong> Generator \u51fd\u6570\u53ef\u4ee5\u901a\u8fc7 <code>yield<\/code> \u5173\u952e\u5b57\u5c06\u51fd\u6570\u7684\u6267\u884c\u6682\u505c\uff0c\u53ef\u4ee5\u7528\u4e8e\u5f02\u6b65\u64cd\u4f5c\u7684\u6d41\u7a0b\u63a7\u5236\uff0c\u4f46\u901a\u5e38\u9700\u8981\u914d\u5408\u5176\u4ed6\u5f02\u6b65\u5904\u7406\u624b\u6bb5\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u548c\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u3002\u6700\u8fd1\u7684 JavaScript \u6807\u51c6\u548c\u5e93\/\u6846\u67b6\u7684\u53d1\u5c55\u4e5f\u4f7f\u5f97\u5904\u7406\u5f02\u6b65\u64cd\u4f5c\u53d8\u5f97\u66f4\u52a0\u7b80\u5355\u548c\u5f3a\u5927\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript \u4e2d\u5b9e\u73b0\u5f02\u6b65\u64cd\u4f5c\u7684\u65b9\u5f0f\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u4e00\u4e9b\u4e3b\u8981\u7684\u65b9\u5f0f\u5305\u62ec\uff1a \u56de\u8c03\u51fd\u6570\uff08Callbacks\uff09\uff1a  [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[115],"tags":[],"class_list":["post-11920","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11920","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\/11"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=11920"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11920\/revisions"}],"predecessor-version":[{"id":13626,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11920\/revisions\/13626"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11920"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11920"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11920"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}