{"id":12458,"date":"2014-04-03T14:48:58","date_gmt":"2014-04-03T09:18:58","guid":{"rendered":"https:\/\/2thenew.online\/blog\/?p=12458"},"modified":"2016-04-30T00:37:43","modified_gmt":"2016-04-29T19:07:43","slug":"async-module-for-node-js","status":"publish","type":"post","link":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/","title":{"rendered":"Async module for node.js"},"content":{"rendered":"<p>In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module <strong>&#8220;async&#8221;<\/strong> that helps us to push down our functions in an array, so that we can execute them as per the need of the project.<\/p>\n<ol><strong>Installation Part:<\/strong><\/ol>\n<p>Run this command to install the &#8220;async&#8221; module through <strong>npm<\/strong> (Node Package Manager)<\/p>\n<p>[js]$npm install async[\/js]<\/p>\n<p>Now this will install the async module in your Project.<\/p>\n<p>After the installation, you can use the predefined functions of this modules by simply passing an array of functions to its methods.For that you need to require the async module in your application.<\/p>\n<p>[js] var async =require(&#8216;async&#8217;);[\/js]<\/p>\n<p>Now all you have to do is simply create an array of functions you want to call either synchronously or asynchronously. And pass this array to a desired method of async module.<\/p>\n<p>[js] Task[] = [&lt;function 1&gt;,&lt;function 2&gt;&#8230;&#8230;..&lt;function n&gt;];[\/js]<\/p>\n<p>Some of the methods of async module that we usually use to control the flow of the tasks are<\/p>\n<ul>\n<li>series<\/li>\n<li>parallel<\/li>\n<li>forever<\/li>\n<li>waterfall<\/li>\n<\/ul>\n<p>Lets see a demonstration of how it works<\/p>\n<ol><strong>Code :<\/strong><\/ol>\n<p>[js] var async =require(&#8216;async&#8217;);<\/p>\n<p>var task=[];<\/p>\n<p>task.push(function(cb){<br \/>\nsetTimeout(function(){console.log(&quot;first&quot;);<br \/>\ncb(null,&quot;first&quot;);<br \/>\n},3000)<\/p>\n<p>});<br \/>\ntask.push(function(cb){<br \/>\nsetTimeout(function(){console.log(&quot;second&quot;);<br \/>\ncb(null,&quot;second&quot;);}<br \/>\n,1000)<\/p>\n<p>});<br \/>\ntask.push(function(cb){<\/p>\n<p>setTimeout(function(){console.log(&quot;final&quot;);<br \/>\ncb(null,&quot;final&quot;);}<br \/>\n,6000)<\/p>\n<p>});<\/p>\n<p>async.series(task,function(err,data){console.log(data)});<br \/>\nasync.parallel(task,function(err,data){console.log(data)});<br \/>\n[\/js]<\/p>\n<p>The series method calls each function of the array task, one by one in a form of series.This makes the execution flow synchronous in nature.<\/p>\n<p>##Output of series :<\/p>\n<p>[js]<br \/>\nfirst<br \/>\nsecond<br \/>\nfinal<br \/>\n[ &#8216;first&#8217;, &#8216;second&#8217;, &#8216;final&#8217; ]<br \/>\n[\/js]<\/p>\n<p>The parallel method calls each function of the array task, simultaneously in a form of parallel execution.This makes the execution flow asynchronous in nature.<\/p>\n<p>##Output of parallel :<\/p>\n<p>[js]<br \/>\nsecond<br \/>\nfirst<br \/>\nfinal<br \/>\n[ &#8216;first&#8217;, &#8216;second&#8217;, &#8216;final&#8217; ]<br \/>\n[\/js]<\/p>\n<p>This is how we manage the function flow ,as per our need in node js. Hope this will help.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module &#8220;async&#8221; that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command [&hellip;]<\/p>\n","protected":false},"author":99,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3,"footnotes":""},"categories":[1185],"tags":[1177],"class_list":["post-12458","post","type-post","status-publish","format-standard","hentry","category-node-js-2","tag-nodejs-2"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module &quot;async&quot; that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Vibhor Kukreja\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Async module for node.js | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module &quot;async&quot; that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Async module for node.js | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module &quot;async&quot; that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#article\",\"name\":\"Async module for node.js | TO THE NEW Blog\",\"headline\":\"Async module for node.js\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/vibhor-kukreja\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2014-04-03T14:48:58+05:30\",\"dateModified\":\"2016-04-30T00:37:43+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#webpage\"},\"articleSection\":\"Node.js, nodejs\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"position\":2,\"name\":\"Node.js\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#listItem\",\"name\":\"Async module for node.js\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#listItem\",\"position\":3,\"name\":\"Async module for node.js\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/node-js-2\\\/#listItem\",\"name\":\"Node.js\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/vibhor-kukreja\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/vibhor-kukreja\\\/\",\"name\":\"Vibhor Kukreja\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b6f356c071a634e6360accc831ed76faed3f121165a8952666c71c81773cbc48?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Vibhor Kukreja\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/\",\"name\":\"Async module for node.js | TO THE NEW Blog\",\"description\":\"In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module \\\"async\\\" that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/async-module-for-node-js\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/vibhor-kukreja\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/vibhor-kukreja\\\/#author\"},\"datePublished\":\"2014-04-03T14:48:58+05:30\",\"dateModified\":\"2016-04-30T00:37:43+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Async module for node.js | TO THE NEW Blog","description":"In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module \"async\" that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command","canonical_url":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#article","name":"Async module for node.js | TO THE NEW Blog","headline":"Async module for node.js","author":{"@id":"https:\/\/2thenew.online\/blog\/author\/vibhor-kukreja\/#author"},"publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"},"datePublished":"2014-04-03T14:48:58+05:30","dateModified":"2016-04-30T00:37:43+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#webpage"},"articleSection":"Node.js, nodejs"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.online\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/#listItem","position":2,"name":"Node.js","item":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#listItem","name":"Async module for node.js"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#listItem","position":3,"name":"Async module for node.js","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/#listItem","name":"Node.js"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.online\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.online\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.online\/blog\/author\/vibhor-kukreja\/#author","url":"https:\/\/2thenew.online\/blog\/author\/vibhor-kukreja\/","name":"Vibhor Kukreja","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/b6f356c071a634e6360accc831ed76faed3f121165a8952666c71c81773cbc48?s=96&d=mm&r=g","width":96,"height":96,"caption":"Vibhor Kukreja"}},{"@type":"WebPage","@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#webpage","url":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/","name":"Async module for node.js | TO THE NEW Blog","description":"In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module \"async\" that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.online\/blog\/author\/vibhor-kukreja\/#author"},"creator":{"@id":"https:\/\/2thenew.online\/blog\/author\/vibhor-kukreja\/#author"},"datePublished":"2014-04-03T14:48:58+05:30","dateModified":"2016-04-30T00:37:43+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.online\/blog\/#website","url":"https:\/\/2thenew.online\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Async module for node.js | TO THE NEW Blog","og:description":"In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module &quot;async&quot; that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command","og:url":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/","og:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Async module for node.js | TO THE NEW Blog","twitter:description":"In our node js application, sometimes we have to execute functions either synchronously or asynchronously. To achieve so , we have a node js module &quot;async&quot; that helps us to push down our functions in an array, so that we can execute them as per the need of the project. Installation Part: Run this command","twitter:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"12458","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 06:08:38","updated":"2024-02-29 09:47:29","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\/category\/node-js-2\/\" title=\"Node.js\">Node.js<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tAsync module for node.js\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.online\/blog"},{"label":"Node.js","link":"https:\/\/2thenew.online\/blog\/category\/node-js-2\/"},{"label":"Async module for node.js","link":"https:\/\/2thenew.online\/blog\/async-module-for-node-js\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/12458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/users\/99"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/comments?post=12458"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/12458\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/media?parent=12458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/categories?post=12458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/tags?post=12458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}