{"id":299,"date":"2021-09-25T14:33:19","date_gmt":"2021-09-25T14:33:19","guid":{"rendered":"https:\/\/www.bhoomabrsr.com\/blog\/?p=299"},"modified":"2021-09-25T14:47:49","modified_gmt":"2021-09-25T14:47:49","slug":"recursive-implementation-for-asyn-series","status":"publish","type":"post","link":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/2021\/09\/25\/recursive-implementation-for-asyn-series\/","title":{"rendered":"Recursive Implementation for Asyn.series"},"content":{"rendered":"\n<p><meta charset=\"utf-8\">We are going to look into a recursive solution for the series method. Note that recursive always comes with a performance dent with a deeper call stack of instruction execution in the hardware.<\/p>\n\n\n\n<p>The idea here will start with a <em>function1<\/em> from the tasks array and then pass a wrapped callback function on reading <em>the result of function1<\/em>, then invoking <em>function2<\/em> and vice versa. If any error occurs in the process, the algorithom will exit by calling the final callback with error and results collection.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n    fn((error, result) =&gt; {\n      if (error) {\n        callback(error, results);\n        return;\n      }\n      results.push(result);\n      \/\/invoke a recursive call to next method \n    });\n\n<\/pre><\/div>\n\n\n<p>The full code is available in the below code block and it is ready to execute in your console for analysis or debug to see how this works in action.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; highlight: [8,23]; title: ; notranslate\" title=\"\">\nconst sampleTasks =  &#x5B; function (callback) { setTimeout(function () { console.log(&quot;1&quot;); callback(null, &quot;one&quot;); }, 200); }, function (callback) { setTimeout(function () { console.log(&quot;2&quot;); callback(null, &quot;two&quot;); }, 100); }, function (callback) { setTimeout(function () { console.log(&quot;3&quot;); callback(null, &quot;three&quot;); }, 90); }, function (callback) { setTimeout(function () { console.log(&quot;4&quot;); callback(Error(&quot;some error&quot;)); }, 10); }, function (callback) { setTimeout(function () { console.log(&quot;5&quot;); callback(null, &quot;five&quot;); }, 1000); }, ];\n \nfunction onSeriesCompleted(error, results) {\n  console.log(&quot;Error stack&quot;, error);\n  console.log(&quot;Results of successfully executed tasks&quot;, results);\n}\n\nfunction startTask(tasks, callback, counter = 0, results = &#x5B;]) {\n  if (counter &lt; tasks.length) {\n    const fn = tasks&#x5B;counter];\n    const fn2 = tasks&#x5B;counter + 1];\n    fn((error, result) =&gt; {\n      if (error) {\n        callback(error, results);\n        return;\n      }\n      results.push(result);\n      startTask(tasks, callback, ++counter, results);\n    });\n  }\n}\n\nfunction series(tasks, finalCallback) {\n  startTask(tasks, finalCallback);\n}\n\nseries(sampleTasks, onSeriesCompleted);\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1168\" height=\"324\" src=\"https:\/\/www.bhoomabrsr.com\/blog\/wp-content\/uploads\/2021\/09\/image-3.png\" alt=\"\" class=\"wp-image-316\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1396\" height=\"1380\" src=\"https:\/\/www.bhoomabrsr.com\/blog\/wp-content\/uploads\/2021\/09\/startTask_series-2.png\" alt=\"\" class=\"wp-image-322\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Recursive Implementation for Asyn.series<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,14,15,33,34],"tags":[],"class_list":["post-299","post","type-post","status-publish","format-standard","hentry","category-async","category-ecma6","category-javascript","category-specifications","category-standards"],"_links":{"self":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":8,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":323,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/299\/revisions\/323"}],"wp:attachment":[{"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bhoomabrsr.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}