Sumit | Javascript + React
Sumit | Javascript + React

@sumitsaurabh927

18 Tweets 2 reads May 28, 2022
๐Ÿ“Œ Asynchronous Javascript:
๐Ÿค” How does Asynchronous Javascript even work?
{A 16 tweets' thread }
๐Ÿ‘† Javascript is a single threaded language.
๐Ÿ‘‰ It can only do one thing at a time.
๐Ÿค” But web requires it to to multiple things at a time.
โœŒ๏ธ Ex- Page should be responsive while API is fetching data in background.
โŒ But Javascript can't handle both the background task as well as keeping the page responsive at the same time.
๐Ÿ’ก Remember, it is a single-threaded language.
โœ… It can only do ONE task at a time.
๐ŸŒŸ This mismatch of our requirements and what Javascript can do gave rise to web APIs.
๐Ÿค— They are sort of mini helpers of Javascript that handle async (background tasks) while Javascript does its 'one' task.
โŒ Web APIs aren't provided natively in Javascript language.
๐Ÿ˜‰ But because Javascript we use runs in a special environment (our browsers), it gets access to the web APIs.
๐Ÿ•บ I.E., browsers provide Javascript the ability to handle async tasks through web APIs.
๐Ÿ‘‰ I've covered why we need asynchronicity in Javascript as web developers here:
๐Ÿ‘‰ I've also explained how Javascript manages to have asynchronous functionality while being a synchronous language here:
โœ… I'm assuming you've read both of the above-linked threads because in this thread I'll be relying on information from both of those and focusing specifically on how this handling of async tasks to web APIs work. In detail.
๐Ÿ“Œ Execution context
๐Ÿ–ผ๏ธ Notice the attached image.
๐ŸŒ† It is the technical landscape of the browser's environment.
๐Ÿ’ญ It has three main parts:
1๏ธโƒฃ Javascript's call stack,
2๏ธโƒฃ Web APIs, and
3๏ธโƒฃ Job queue
๐Ÿ”ฌ Let's look at each one of them, in detail, below.
๐Ÿ“Œ Call stack:
โ˜Ž๏ธ The Stack is a Last In First Out data structure (LIFO).
โฌ…๏ธ Meaning data that enters last is the first one to leave.
๐Ÿ“š Think of a stack of books. The book that was placed at the end (top) is the first to get picked up.
๐ŸŒŸ It helps JS do its 'one' thing
๐Ÿ“Œ Queue:
โžก๏ธ A queue is a First In First Out data structure (FIFO).
๐Ÿ’ก Meaning data that enters first leaves first.
๐Ÿคญ I think you've been in enough queues to know that the first one in is the first one out!
๐Ÿง  Now the meaty part:
โš™๏ธ In our browsers, Javascript runs inside a Javascript engine.
๐Ÿš€ For Chrome, it is the V8 engine.
โŒ This engine isn't a compiler because it has more things like memory management, garbage collection etc.
๐Ÿ›ก๏ธ Now, when the JS call stack encounters an async task(async function at the top of JS stack), it shifts it to the Web API (async function popped off from stack).
โšก Now, JS stack is free to do more execution (keeping the page responsive).
๐Ÿค Meanwhile the Web APIs performs the task and pushes the result on to the job queue.
๐Ÿค— The job queue holds the result (and further results from more async requests keep lining up in the queue)
๐Ÿ“ The Event loop keeps checking if the call stack of JS has become empty or not.
๐Ÿ“ If it is, it takes one result (whatever entered the job queue first, since queue is FIFO) and pushes it to JS stack.
๐ŸคŒ The JS stack now has an item to deal with (meaning stack is non-empty, so event loop can't push more)
๐Ÿงƒ And since JS is single-threaded (and stack is LIFO) => it can only do one thing (the pushed result is on top so it'll be dealt with) => it deals with the result.
๐Ÿคž When it is done, event loop again notices that the stack is empty and takes the 'latest' stuff from job queue and gives it to JS' stack for JS to deal with it.
โค๏ธ This is how async tasks are handled by Javascript!
I write daily threads on web dev so if you enjoyed this thread, please:
โœ… Follow me @sumitsaurabh927 for more of these.
โœ… RT the tweet below to show me some ๐Ÿงก and comment your thoughts.
๐ŸŒŸ Your each like, every comment motivates me to come up with awesome new stuff!

Loading suggestions...