JS Promises & Async/Await: What you NEED to know

Published: 03 October 2022
on channel: Kodaps Academy
486
41

JavaScript's problem is that it is single-threaded.
What do we mean by that? It means that if it does something that takes time - an intensive calculation for example - the JS engine cannot do anything else at the same time.
Sometimes I code a "while" block and I forget to do anything to the variable on which I wrote the condition. And so I create an infinite loop. When I do this, … everything freezes. The browser doesn't respond anymore, I have to force the window to close. This is an extreme case, but as you probably know, movies run at 24 frames per second, and video games at 60 FPS or more. But 24 frames per second means one frame every 40 milliseconds or so. (1/24 = 0.0416 )
So any process or calculation in #JavaScript must get the job done in 40 milliseconds or less. Otherwise, users will get the impression that the application is "lagging".
But if you want to call an API, on a web server, the message's round trip is bound to take longer than that. And JavaScript just can't afford to wait. So we need to find ways to tell it to go and do other stuff while waiting. 
The solution.. is #Promises.

check out: https://www.kodaps.dev/what-are-javas...