

{"id":100666,"date":"2021-10-07T09:00:48","date_gmt":"2021-10-07T03:30:48","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=100666"},"modified":"2021-10-07T11:55:20","modified_gmt":"2021-10-07T06:25:20","slug":"nodejs-callback-concept","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/","title":{"rendered":"Nodejs Callback Concept"},"content":{"rendered":"<p>In this tutorial, we will be discussing Nodejs callback concept and its type in detail with code and examples.<\/p>\n<h3>What is Callback function in Nodejs?<\/h3>\n<p>Callback is like a function. Sometimes when a task is completed, a function is called and this function is known as callback function. Using callbacks, nodejs can process the requests without waiting for the results of other functions.<\/p>\n<h3>Working of Nodejs Callback:<\/h3>\n<p>The function contains the callback function as its parameter, now since the callback function is present inside a function it can be executed whenever it is required.<\/p>\n<h3>Nodejs Synchronous functions:<\/h3>\n<p>This function blocks the thread until this function itself executes completely. If we are reading a very large file using the synchronous approach then it will take some time and rest all the tasks will have to wait until the completion of reading the file. Look at the example code of blocking callbacks below to see how it behaves.<\/p>\n<h3>Nodejs Asynchronous functions:<\/h3>\n<p>This function does not block the thread. Even when a function is not completely executed other codes can perform their task parallely. Here two or more tasks run simultaneously. Look at the example code of non-blocking callbacks below to see how it behaves.<\/p>\n<h3>Types of callback in Nodejs:<\/h3>\n<p>1) Blocking callbacks<br \/>\n2) Non-Blocking callback<\/p>\n<h4>Nodejs Blocking callbacks:<\/h4>\n<p>For demonstrating the blocking callbacks we have created a DataFlair.txt file containing \u201cWelcome to DataFlair\u201d, next in the index.js add the below code of blocking callbacks<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require(\"fs\");\r\nvar data = fs.readFileSync('DataFlair.txt');\r\nconsole.log(data.toString());\r\nconsole.log(\"Program Ended\");\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-blocking-callback.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-100935\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-blocking-callback.png\" alt=\"nodejs blocking callback\" width=\"1366\" height=\"726\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-blocking-callback.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-blocking-callback-768x408.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-blocking-callback-720x383.png 720w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-blocking-callback-520x276.png 520w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-blocking-callback-320x170.png 320w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>From the output, you can observe that it has first read the file and then the subsequent codes were executed i.e the reading of file has blocked the execution of other lines.<\/p>\n<h4>Nodejs Non-Blocking Callbacks:<\/h4>\n<p>In non-blocking callbacks the other codes will keep executing and will not wait for the completion of the callback function.<\/p>\n<p><strong>Code of non blocking callbacks:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">var fs = require(\"fs\");\r\nfs.readFile('DataFlair.txt', function (err, data) {\r\n    if (err) return console.error(err);\r\n    console.log(data.toString());\r\n});\r\n \r\nconsole.log(\"Program Ended\");\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>Here we can see that the last line got printed first and then the other codes were executed. It is happening like this because when the file system was reading the text file the execution of other codes did not stop, hence the last line got executed first and then when the result of the callback was ready, it got executed.<\/p>\n<h3>Conclusion:<\/h3>\n<p>Observing the above outputs we can see that blocking code executes sequentially whereas a non blocking code does not. And also most importantly if some block of code needs to use the result of some other function then we should write it in a sequential approach.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will be discussing Nodejs callback concept and its type in detail with code and examples. What is Callback function in Nodejs? Callback is like a function. Sometimes when a task&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":100933,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25285,25281,25282,25283,25284],"class_list":["post-100666","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-nodejs-blocking-callbacks","tag-nodejs-callback","tag-nodejs-callback-concept","tag-nodejs-callbacks","tag-nodejs-non-blocking-callbacks"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nodejs Callback Concept - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what is Nodejs callback concept. See the types of callbacks like Blocking callbacks and Non-Blocking callback with examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nodejs Callback Concept - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what is Nodejs callback concept. See the types of callbacks like Blocking callbacks and Non-Blocking callback with examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-07T03:30:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-07T06:25:20+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-callback-concept.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nodejs Callback Concept - DataFlair","description":"Learn what is Nodejs callback concept. See the types of callbacks like Blocking callbacks and Non-Blocking callback with examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/","og_locale":"en_US","og_type":"article","og_title":"Nodejs Callback Concept - DataFlair","og_description":"Learn what is Nodejs callback concept. See the types of callbacks like Blocking callbacks and Non-Blocking callback with examples.","og_url":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-10-07T03:30:48+00:00","article_modified_time":"2021-10-07T06:25:20+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-callback-concept.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Nodejs Callback Concept","datePublished":"2021-10-07T03:30:48+00:00","dateModified":"2021-10-07T06:25:20+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/"},"wordCount":417,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-callback-concept.jpg","keywords":["Nodejs Blocking callbacks","Nodejs Callback","Nodejs Callback concept","Nodejs Callbacks","Nodejs Non-Blocking Callbacks"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/","url":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/","name":"Nodejs Callback Concept - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-callback-concept.jpg","datePublished":"2021-10-07T03:30:48+00:00","dateModified":"2021-10-07T06:25:20+00:00","description":"Learn what is Nodejs callback concept. See the types of callbacks like Blocking callbacks and Non-Blocking callback with examples.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-callback-concept.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/09\/nodejs-callback-concept.jpg","width":1200,"height":628,"caption":"nodejs callback concept"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/nodejs-callback-concept\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Nodejs Callback Concept"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=100666"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100666\/revisions"}],"predecessor-version":[{"id":100938,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/100666\/revisions\/100938"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/100933"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=100666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=100666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=100666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}