Sumit | Javascript + React
Sumit | Javascript + React

@sumitsaurabh927

11 Tweets 2 reads May 28, 2022
๐Ÿ“Œ Asynchronous Javascript:
๐Ÿ’ก Over 50% of wev developers don't know about this!
๐Ÿค” How does Javascript manage to do something that can not be done by a single threaded language despite being one?
๐Ÿง  The answer, it doesn't!
{ A thread }
๐Ÿ‘‰ First some context:
โœ… Read this thread to know why async functionality is important on the web:
๐Ÿค” Now the logical next question is: How does Javascript behave like async when it actually is synchronous and has a single stack?
๐Ÿ‘‡ Read along to find out!
๐ŸŽฏ So how exactly does Javascript manage to do something that can not be done by a single threaded language despite being one?
โŒ The answer is - it doesn't.
๐Ÿ… Javscript is synchronous and it can't do async tasks!
๐Ÿคญ Javascript doesn't do any asynchronous task.
๐Ÿ’ญ Remember the paperboy analogy from my previous thread ( linked above ).
๐Ÿšฒ The paperboy throws in the newspaper, moves to the next house while his helper waits for the payment.
๐Ÿคฏ In the browser world, Javascript takes an asynchronous task and gives it to the Web APIs.
๐Ÿค Web APIs are the helper of our paperboy in the world of the web.
๐Ÿ’ก And just like it is the job of the helper to collect the payment on behalf of paperboy, in the browser it is the job of web APIs to run the async task and report back the results while Javascript keeps on executing more code.
๐ŸŒŸ So, essentially Javascript manages to do asynchronous tasks despite being a synchronous language because it is assisted by a magical helper called Web API.
โšก And it is browsers that provide Javascript with the web API.
โœ… Vanilla Javascript doesn't have it on its own!
So, Javascript sees async tasks
โ†“
gives it to web APIs
โ†“
they run it in background while Javascript continues executing more code
โ†“
when done, web APIs push output to job queue
โ†“
event loop runs on every iteration and gives those results back to Javascript.
๐Ÿ’ก And that's how Javascript manage to handle asynchronous tasks despite being a synchronous language.
โœ… I'll be doing another thread on how web APIs work and explain call stack, event loop, job queue in detail, so make sure you have tweet notifications turned on.
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...