{"id":53602,"date":"2024-12-03T09:49:14","date_gmt":"2024-12-03T01:49:14","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53602\/"},"modified":"2024-12-03T09:49:14","modified_gmt":"2024-12-03T01:49:14","slug":"%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%b9%b6%e5%8f%91%e6%b5%81%e4%b8%ad%e6%af%8f%e4%b8%aa%e4%bb%bb%e5%8a%a1%e7%9a%84%e5%93%8d%e5%ba%94%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53602\/","title":{"rendered":"\u5982\u4f55\u83b7\u53d6\u5e76\u53d1\u6d41\u4e2d\u6bcf\u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u83b7\u53d6\u5e76\u53d1\u6d41\u4e2d\u6bcf\u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\uff1f<\/h1>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u83b7\u53d6\u5e76\u53d1\u6d41\u4e2d\u6bcf\u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\uff1f\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733131667674d7d93b2c2a.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u83b7\u53d6\u5e76\u53d1\u6d41\u4e2d\u6bcf\u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u83b7\u53d6\u5e76\u53d1\u6d41\u4e2d\u6bcf\u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u63a7\u5236\u5e76\u53d1\u6d41\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u54cd\u5e94<\/strong><\/p>\n<p>javascript \u4e2d\u5e76\u884c\u63a7\u5236\u7ecf\u5e38\u7528\u4e8e\u7ba1\u7406\u5e76\u53d1\u8bf7\u6c42\uff0c\u9650\u5236\u5e76\u884c\u6267\u884c\u7684\u4efb\u52a1\u6570\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u5df2\u5b9e\u73b0\u4e86\u4e00\u4e2a\u63a7\u5236\u5e76\u53d1\u6570\u7684\u51fd\u6570 createpool\u3002\u5b83\u5c06\u4efb\u52a1\u653e\u5165\u961f\u5217\u4e2d\uff0c\u5e76\u63a7\u5236\u540c\u65f6\u6267\u884c\u7684\u4efb\u52a1\u6570\u3002<\/p>\n<p>\u95ee\u9898\u5728\u4e8e\uff0c\u60f3\u8981\u5728 for \u5faa\u73af\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u6267\u884c\u540e\u7684\u54cd\u5e94\uff0c\u4f46\u76ee\u524d\u53ea\u80fd\u83b7\u53d6\u5230\u6700\u5f00\u59cb 5 \u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a<\/p>\n<ul>\n<li>\u540c\u6b65 for \u5faa\u73af\u4ea7\u751f\u4e86 100 \u4e2a\u5e76\u53d1\u4efb\u52a1\uff0c\u4f46\u5e76\u53d1\u6570\u9650\u5b9a\u4e86\u53ea\u6709 5 \u4e2a\u4efb\u52a1\u53ef\u540c\u65f6\u6267\u884c\u3002<\/li>\n<li>\u56e0\u6b64\uff0c\u5269\u4f59 95 \u4e2a\u4efb\u52a1\u7684 promise \u88ab\u4e22\u5f03\uff0c\u5bfc\u81f4\u65e0\u6cd5\u83b7\u53d6\u5176\u54cd\u5e94\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u6620\u5c04\uff08map\uff09\u6765\u7f13\u5b58\u6bcf\u4e2a\u4efb\u52a1\u7684 promise \u7684 resolve \u548c reject \u51fd\u6570\u3002\u4fee\u6539\u540e\u7684 createpool \u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n<pre>const createpool = (task, { concurrency } = {}) =&amp;gt; {\n  let runningcount = 0;\n  const pool = [];\n  const promisemap = new map();\n\n  return function (i) {\n    return new promise((resolve, reject) =&amp;gt; {\n      promisemap.set(i, { resolve, reject });\n\n      pool.push(() =&amp;gt; task(i));\n\n      function run() {\n        while (pool.length &amp;amp;&amp;amp; runningcount &amp;lt; concurrency) {\n          const runtask = pool.shift();\n          runningcount++;\n          runtask()\n            .then((val) =&amp;gt; {\n              const { resolve } = promisemap.get(val)\n              resolve(val)\n            })\n            .catch((e) =&amp;gt; reject(e))\n            .finally(() =&amp;gt; {\n              runningcount--;\n              run();\n            });\n        }\n      }\n      run();\n    });\n  };\n};<\/pre>\n<p>\u73b0\u5728\uff0c\u5728 for \u5faa\u73af\u4e2d\uff0c\u53ef\u4ee5\u8bbf\u95ee\u6bcf\u4e2a\u4efb\u52a1\u7684\u6620\u5c04 promise\uff1a<\/p>\n<pre>  for (let i = 0; i &amp;lt; 100; i++) {\n    fetchData(i).then((response) =&amp;gt; {\n      console.log(&amp;quot;response ---&amp;quot;, response);\n    });\n  }<\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u6bcf\u4e2a\u4efb\u52a1\u6267\u884c\u540e\u7684\u54cd\u5e94\u4e86\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5982\u4f55\u83b7\u53d6\u5e76\u53d1\u6d41\u4e2d\u6bcf\u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\uff1f\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u7535\u8111\u84dd\u5c4f\uff1a\u5982\u4f55\u5feb\u901f\u89e3\u51b3Windows\u7cfb\u7edf\u84dd\u5c4f\u9519\u8bef\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u83b7\u53d6\u5e76\u53d1\u6d41\u4e2d\u6bcf\u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\uff1f \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300a\u5982\u4f55\u83b7\u53d6\u5e76\u53d1\u6d41\u4e2d\u6bcf\u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\uff1f\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ \u63a7\u5236\u5e76\u53d1\u6d41\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u54cd\u5e94 javascript \u4e2d\u5e76\u884c\u63a7\u5236\u7ecf\u5e38\u7528\u4e8e\u7ba1\u7406\u5e76\u53d1\u8bf7\u6c42\uff0c\u9650\u5236\u5e76\u884c\u6267\u884c\u7684\u4efb\u52a1\u6570\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u5df2\u5b9e\u73b0\u4e86\u4e00\u4e2a\u63a7\u5236\u5e76\u53d1\u6570\u7684\u51fd\u6570 createpool\u3002\u5b83\u5c06\u4efb\u52a1\u653e\u5165\u961f\u5217\u4e2d\uff0c\u5e76\u63a7\u5236\u540c\u65f6\u6267\u884c\u7684\u4efb\u52a1\u6570\u3002 \u95ee\u9898\u5728\u4e8e\uff0c\u60f3\u8981\u5728 for \u5faa\u73af\u4e2d\u83b7\u53d6\u6bcf\u4e2a\u4efb\u52a1\u6267\u884c\u540e\u7684\u54cd\u5e94\uff0c\u4f46\u76ee\u524d\u53ea\u80fd\u83b7\u53d6\u5230\u6700\u5f00\u59cb 5 \u4e2a\u4efb\u52a1\u7684\u54cd\u5e94\u3002\u8fd9\u662f\u56e0\u4e3a\uff1a \u540c\u6b65 for \u5faa\u73af\u4ea7\u751f\u4e86 100 \u4e2a\u5e76\u53d1\u4efb\u52a1\uff0c\u4f46\u5e76\u53d1\u6570\u9650\u5b9a\u4e86\u53ea\u6709 5 \u4e2a\u4efb\u52a1\u53ef\u540c\u65f6\u6267\u884c\u3002 \u56e0\u6b64\uff0c\u5269\u4f59 95 \u4e2a\u4efb\u52a1\u7684 promise \u88ab\u4e22\u5f03\uff0c\u5bfc\u81f4\u65e0\u6cd5\u83b7\u53d6\u5176\u54cd\u5e94\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u6620\u5c04\uff08map\uff09\u6765\u7f13\u5b58\u6bcf\u4e2a\u4efb\u52a1\u7684 promise \u7684 resolve \u548c reject \u51fd\u6570\u3002\u4fee\u6539\u540e\u7684 createpool \u51fd\u6570\u5982\u4e0b\uff1a const createpool = (task, { concurrency } = {}) =&amp;gt; { let runningcount = 0; const pool = []; const promisemap = [&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-53602","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53602","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=53602"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53602\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}