{"id":64421,"date":"2024-05-30T21:30:04","date_gmt":"2024-05-30T18:00:04","guid":{"rendered":"https:\/\/nabfollower.com\/blog\/%d9%85%d8%ad%d8%af%d9%88%d8%af%db%8c%d8%aa-%d9%87%d9%85%d8%b2%d9%85%d8%a7%d9%86%db%8c\/"},"modified":"2024-05-30T21:30:04","modified_gmt":"2024-05-30T18:00:04","slug":"%d9%85%d8%ad%d8%af%d9%88%d8%af%db%8c%d8%aa-%d9%87%d9%85%d8%b2%d9%85%d8%a7%d9%86%db%8c","status":"publish","type":"post","link":"https:\/\/nabfollower.com\/blog\/%d9%85%d8%ad%d8%af%d9%88%d8%af%db%8c%d8%aa-%d9%87%d9%85%d8%b2%d9%85%d8%a7%d9%86%db%8c\/","title":{"rendered":"\u0645\u062d\u062f\u0648\u062f\u06cc\u062a \u0647\u0645\u0632\u0645\u0627\u0646\u06cc"},"content":{"rendered":"<p><\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>const CONCURRENCY_LIMIT = 3\n\nfunction asyncTaskRunner(tasks) {\n const results = new Array(tasks.length).fill(0)\n const totalTasks = tasks.length\n let currentTaskIndex = 0\nconst runner = (resolve) =&gt; {\n   if (!Array.isArray(tasks)) {\n     throw new Error(\"tasks must be Array\");\n   }\n   let taskQueue = []\n   const runNext = async () =&gt; {\n     if (taskQueue.length === 0 &amp;&amp; currentTaskIndex === totalTasks) {\n       resolve(results)\n       return\n     }\n\n     if (taskQueue.length &gt;= CONCURRENCY_LIMIT || tasks.length === 0) {\n       return\n     }\n\n\n     const taskIndex = currentTaskIndex;\n     currentTaskIndex += 1\n\n     const task = tasks.shift()\n     console.log('scheduling task ', taskIndex)\n     taskQueue.push(task)\n\n     task()\n       .then(result =&gt; {\n         console.log('finished task: ', taskIndex)\n         results[taskIndex] = result\n         taskQueue = taskQueue.filter((t =&gt; t !== task))\n         runNext()\n       })\n   }\n   for (let i = 0 ; i &lt; CONCURRENCY_LIMIT; i++) {\n     if (tasks.length === 0) {\n       break\n     }\n\n\n     const taskIndex = currentTaskIndex;\n     currentTaskIndex += 1\n\n\n     const task = tasks.shift()\n     console.log('scheduling task ', taskIndex)\n     taskQueue.push(task)\n\n\n     task()\n       .then(result =&gt; {\n         console.log('finished task: ', taskIndex)\n         results[taskIndex] = result\n         taskQueue = taskQueue.filter((t =&gt; t !== task))\n         runNext()\n       })\n   }\n };\n return new Promise((res) =&gt; runner(res));\n}\n\n\nconst t1 = () =&gt; new Promise(res =&gt; setTimeout(() =&gt; res('t1'), 3000))\nconst t2 = () =&gt; new Promise(res =&gt; setTimeout(() =&gt; res('t2'), 200))\nconst t3 = () =&gt; new Promise(res =&gt; setTimeout(() =&gt; res('t3'), 1500))\nconst t4 = () =&gt; new Promise(res =&gt; setTimeout(() =&gt; res('t4'), 5000))\nconst t5 = () =&gt; new Promise(res =&gt; setTimeout(() =&gt; res('t5'), 4000))\nconst t6 = () =&gt; new Promise(res =&gt; setTimeout(() =&gt; res('t6'), 1000))\n\n\nconst tasks = [t1, t2, t3, t4, t5, t6]\n\n\nasyncTaskRunner(tasks)\n .then(console.log)\n\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>const CONCURRENCY_LIMIT = 3 function asyncTaskRunner(tasks) { const results = new Array(tasks.length).fill(0) const totalTasks = tasks.length let currentTaskIndex = 0 const runner = (resolve) =&gt; { if (!Array.isArray(tasks)) { throw new Error(&#8220;tasks must be Array&#8221;); } let taskQueue = [] const runNext = async () =&gt; { if (taskQueue.length === 0 &amp;&amp; currentTaskIndex === totalTasks) &hellip;<\/p>\n","protected":false},"author":2,"featured_media":64422,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/media.dev.to\/cdn-cgi\/image\/width=1000,height=500,fit=cover,gravity=auto,format=auto\/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dtlec3gqbv7qfe6f2fz.png","fifu_image_alt":"","footnotes":""},"categories":[339],"tags":[],"class_list":["post-64421","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/64421","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/comments?post=64421"}],"version-history":[{"count":0,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/64421\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media\/64422"}],"wp:attachment":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media?parent=64421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/categories?post=64421"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/tags?post=64421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}