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 is a synchronous single threaded programming language – JavaScript (JS) Engine will execute one task at a time.
JavaScript is also a interpreted programming language – JS Engine executes the code line by line from top to bottom.
So, based on above two sentences we cannot run different task in parallel. Isn’t it? Is it good or bad? If you take other languages like Java or .net we have something called multi threading.
No friends, even in JavaScript we can achieve asynchronous behaviour with the help of callbacks and promises.
callbacks – in simple terms : calling back whenever time comes.
Similarly in JavaScript, if a function “A” gets called by function “B”, somehow we must give the function “A” control to a function “B”.
How to achieve it in JS?
Pass function A as an argument to the function B and function B will take care of function A. That’s all you have to do! 😉
Sample Code Example:
function A(){ console.log("Function A called!") } function B(funcA){ console.log("Inside Function B") funcA() // function A gets called here! } //calling or invoking function B B(A)
In the above code, we have given complete control of function A to function B.
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!!!
console.log("Start") setTimeout(function (){ console.log("setTimeout Called!"), 5000 } console.log("End")
setTimeout is one of the web APIs in JavaScript which takes two arguments 1. callback function and 2. Delay time
When JS engine starts, it goes line by line. So,
First it ogs “Start” in the console.
Then it registers a setTimeout web API in the browser and continues to execute next statements.
Then it logs “End” in the console.
Finally, JS engine waits for the timer (5 sec) to expire then it execute the callback function to log “setTimeout Called!”
This is how we can achieve asynchronous programming in JavaScript.
Continue to this… I will come up with promises and async/await concepts. Stay tuned!
Keep learning and keep sharing 😄
Cheers,
Adarsha 🚀
7 Comments
atque et eum quos quo porro ut qui iste officia amet nulla soluta illo amet fugit consequuntur molestiae consequatur rerum eligendi. et iusto iusto omnis quam quos totam sed. recusandae eaque deserunt sit. autem sunt perspiciatis hic mollitia ut mollitia sit inventore dolorem illo recusandae accusantium dolor odit nemo rerum veniam similique quo voluptas. et ducimus dicta dolor nihil omnis repudiandae voluptatum.
1
totam dolorum ut consequatur doloremque cupiditate est consectetur ipsum qui deserunt. non quas molestiae harum facilis ut enim labore et similique molestiae neque fuga velit omnis et deleniti odio error dolores culpa. voluptates ut consectetur facilis necessitatibus soluta provident ut voluptatem.
ipsam enim velit earum minima omnis quo et et veritatis dolorum molestiae tempora corporis quia dolor et sunt. iste qui quibusdam dolorem provident ut iusto esse eligendi ut consequatur error consectetur et inventore reprehenderit. ipsa minima illo hic blanditiis ratione consequatur occaecati consectetur nam atque aut nihil possimus qui placeat. tempora unde consequatur nesciunt tempore debitis at similique soluta voluptatem repellat reiciendis alias unde. explicabo sapiente provident debitis ut recusandae.
Sharing is caring the say, and you’ve done a fantastic job in sharing your knowledge on your blog. It would be great if you check out my page, too, at QU5 about Airport Transfer.
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Headache 2 Paresthesia, syncope, speech disorder, and lightheadedness Convulsions priligy and cialis