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

preview_player
Показать описание
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.

Рекомендации по теме
Комментарии
Автор

Some of the best JS videos on internet. You deserve way more subscribers, keep up the good work!!

shyamaghara
Автор

Great video! Funny you released this just today, I just released the package resolvjs that turns that last solution into a oneliner( with top-level await )

godbleak
Автор

Loved the video, thanks a bunch for the great simple explanation

raheemamer