{"id":9886,"date":"2023-11-28T21:18:21","date_gmt":"2023-11-28T13:18:21","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9886"},"modified":"2023-11-30T19:55:03","modified_gmt":"2023-11-30T11:55:03","slug":"javascript-zhong-han-shufnl-han-shufn2han-shufn3-r","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/javascript-zhong-han-shufnl-han-shufn2han-shufn3-r\/","title":{"rendered":"JavaScript \u4e2d\u51fd\u6570 fnl\u51fd\u6570 fn2\u51fd\u6570 fn3\uff0c\u5982\u679c\u60f3\u5728\u4e09\u4e2a\u51fd\u6570\u90fd\u6267\u884c\u5b8c\u6210\u540e\u6267\u884c\u67d0\u4e00\u4e2a\u4e8b\u4ef6\u5e94\u8be5\u5982\u4f55\u5b9e\u73b0\uff1f"},"content":{"rendered":"<p>\u5728JavaScript\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u3001Promise\u3001async\/await\u7b49\u65b9\u5f0f\u6765\u786e\u4fdd\u5728\u4e09\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\u518d\u6267\u884c\u67d0\u4e2a\u4e8b\u4ef6\u3002<\/p>\n<h3><a id=\"%E4%BD%BF%E7%94%A8%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528\u56de\u8c03\u51fd\u6570\uff1a<\/h3>\n<pre><code class=\"language-javascript\">function fn1(callback) {\n  \/\/ \u51fd\u6570 fn1 \u7684\u903b\u8f91\n  \/\/ ...\n  callback();\n}\n\nfunction fn2(callback) {\n  \/\/ \u51fd\u6570 fn2 \u7684\u903b\u8f91\n  \/\/ ...\n  callback();\n}\n\nfunction fn3(callback) {\n  \/\/ \u51fd\u6570 fn3 \u7684\u903b\u8f91\n  \/\/ ...\n  callback();\n}\n\n\/\/ \u5728\u4e09\u4e2a\u51fd\u6570\u90fd\u6267\u884c\u5b8c\u6210\u540e\u6267\u884c\u67d0\u4e2a\u4e8b\u4ef6\nfn1(function() {\n  fn2(function() {\n    fn3(function() {\n      \/\/ \u5728\u4e09\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\u6267\u884c\u7684\u4e8b\u4ef6\n      console.log('All functions executed.');\n    });\n  });\n});\n<\/code><\/pre>\n<h3><a id=\"%E4%BD%BF%E7%94%A8promise%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528 Promise\uff1a<\/h3>\n<pre><code class=\"language-javascript\">function fn1() {\n  return new Promise((resolve) =&gt; {\n    \/\/ \u51fd\u6570 fn1 \u7684\u903b\u8f91\n    \/\/ ...\n    resolve();\n  });\n}\n\nfunction fn2() {\n  return new Promise((resolve) =&gt; {\n    \/\/ \u51fd\u6570 fn2 \u7684\u903b\u8f91\n    \/\/ ...\n    resolve();\n  });\n}\n\nfunction fn3() {\n  return new Promise((resolve) =&gt; {\n    \/\/ \u51fd\u6570 fn3 \u7684\u903b\u8f91\n    \/\/ ...\n    resolve();\n  });\n}\n\n\/\/ \u5728\u4e09\u4e2a\u51fd\u6570\u90fd\u6267\u884c\u5b8c\u6210\u540e\u6267\u884c\u67d0\u4e2a\u4e8b\u4ef6\nPromise.all([fn1(), fn2(), fn3()]).then(() =&gt; {\n  \/\/ \u5728\u4e09\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\u6267\u884c\u7684\u4e8b\u4ef6\n  console.log('All functions executed.');\n});\n<\/code><\/pre>\n<h3><a id=\"%E4%BD%BF%E7%94%A8asyncawait%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u4f7f\u7528 async\/await\uff1a<\/h3>\n<pre><code class=\"language-javascript\">async function fn1() {\n  \/\/ \u51fd\u6570 fn1 \u7684\u903b\u8f91\n  \/\/ ...\n}\n\nasync function fn2() {\n  \/\/ \u51fd\u6570 fn2 \u7684\u903b\u8f91\n  \/\/ ...\n}\n\nasync function fn3() {\n  \/\/ \u51fd\u6570 fn3 \u7684\u903b\u8f91\n  \/\/ ...\n}\n\n\/\/ \u5728\u4e09\u4e2a\u51fd\u6570\u90fd\u6267\u884c\u5b8c\u6210\u540e\u6267\u884c\u67d0\u4e2a\u4e8b\u4ef6\n(async () =&gt; {\n  await fn1();\n  await fn2();\n  await fn3();\n  \/\/ \u5728\u4e09\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\u6267\u884c\u7684\u4e8b\u4ef6\n  console.log('All functions executed.');\n})();\n<\/code><\/pre>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u786e\u4fdd\u5728\u4e09\u4e2a\u51fd\u6570\u90fd\u6267\u884c\u5b8c\u6210\u540e\u518d\u6267\u884c\u67d0\u4e2a\u4e8b\u4ef6\uff0c\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u53d6\u51b3\u4e8e\u4f60\u7684\u9879\u76ee\u9700\u6c42\u548c\u4e2a\u4eba\u504f\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728JavaScript\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u3001Promise\u3001async\/await\u7b49\u65b9\u5f0f\u6765\u786e\u4fdd\u5728\u4e09\u4e2a\u51fd\u6570\u6267\u884c [&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-9886","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9886","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=9886"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9886\/revisions"}],"predecessor-version":[{"id":13703,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9886\/revisions\/13703"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9886"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}