{"id":4986,"date":"2024-11-19T23:43:20","date_gmt":"2024-11-19T18:13:20","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=4986"},"modified":"2024-11-19T23:43:21","modified_gmt":"2024-11-19T18:13:21","slug":"asynchronous-programming-in-javascript-3","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/asynchronous-programming-in-javascript-3\/","title":{"rendered":"asynchronous programming in JavaScript."},"content":{"rendered":"<p><\/p>\n<p>Hi All,<\/p>\n<p>I would like to take this opportunity and explain you the asynchronous programming in JavaScript.<\/p>\n<p>Before diving deep into asynchronous concepts like callbacks, promises, async\/await.<\/p>\n<p><strong>Javascript<\/strong>&nbsp;is a synchronous single threaded programming language \u2013 JavaScript (JS) Engine will execute one task at a time.<\/p>\n<p>JavaScript is also a&nbsp;<strong>interpreted<\/strong>&nbsp;programming language \u2013 JS Engine executes the code line by line from top to bottom.<\/p>\n<p>So, based on above two sentences we cannot run different task in parallel. Isn\u2019t it? Is it good or bad? If you take other languages like Java or .net we have something called multi threading.<\/p>\n<p>No friends, even in JavaScript we can achieve asynchronous behaviour with the help of callbacks and promises.<\/p>\n<p>callbacks \u2013 in simple terms : calling back whenever time comes.<\/p>\n<p>Similarly in JavaScript, if a function \u201cA\u201d gets called by function \u201cB\u201d, somehow we must give the function \u201cA\u201d control to a function \u201cB\u201d.<\/p>\n<p>How to achieve it in JS?<\/p>\n<p><em>Pass function A as an argument to the function B and function B will take care of function A. That\u2019s all you have to do! \ud83d\ude09<\/em><\/p>\n<p>Sample Code Example:<\/p>\n<p><\/p>\n<p>function A(){ console.log(&#8220;Function A called!&#8221;) } function B(funcA){ console.log(&#8220;Inside Function B&#8221;) funcA() \/\/ function A gets called here! } \/\/calling or invoking function B B(A)<\/p>\n<p>In the above code, we have given complete control of function A to function B.<\/p>\n<p>We will go to next level to understand asynchronous with the help of callbacks. I would strongly say that, in JavaScript asynchronous behaviour exist only because of callbacks!!!<\/p>\n<p>console.log(&#8220;Start&#8221;) setTimeout(function (){ console.log(&#8220;setTimeout Called!&#8221;), 5000 } console.log(&#8220;End&#8221;)<\/p>\n<p>setTimeout is one of the web APIs in JavaScript which takes two arguments 1. callback function and 2. Delay time<\/p>\n<p>When JS engine starts, it goes line by line. So,<\/p>\n<p>First it ogs \u201cStart\u201d in the console.<\/p>\n<p>Then it registers a setTimeout web API in the browser and continues to execute next statements.<\/p>\n<p>Then it logs \u201cEnd\u201d in the console.<\/p>\n<p>Finally, JS engine waits for the timer (5 sec) to expire then it execute the callback function to log \u201csetTimeout Called!\u201d<\/p>\n<p>This is how we can achieve asynchronous programming in JavaScript.<\/p>\n<p>Continue to this\u2026 I will come up with promises and async\/await concepts. Stay tuned!<\/p>\n<p>Keep learning and keep sharing \ud83d\ude04<\/p>\n<p>Cheers,<\/p>\n<p>Adarsha \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi All, I would like to take this opportunity and explain you the asynchronous programming in JavaScript. Before diving deep into asynchronous concepts like callbacks, promises, async\/await. Javascript&nbsp;is a synchronous single threaded programming language \u2013 JavaScript (JS) Engine will execute one task at a time. JavaScript is also a&nbsp;interpreted&nbsp;programming language \u2013 JS Engine executes the<\/p>\n","protected":false},"author":56,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[333,172],"tags":[330],"class_list":["post-4986","post","type-post","status-publish","format-standard","category-asynchronous-javascript","category-javascript","tag-javascript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4986","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/56"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=4986"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4986\/revisions"}],"predecessor-version":[{"id":5006,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4986\/revisions\/5006"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=4986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=4986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=4986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}