Here you have an excellent #javascript interview question
Here you have an excellent #javascript interview question.
What on earth is async/await doing under the hood?
I'm glad you asked...
JavaScript is an asynchronous language, which means that each line of code is executed one after the other.
JavaScript knows the order in which functions should be executed according to its call stack.
Functions are loaded onto the call stack, and the first function that enters is the first to exit.
It's important to note that the call stack prioritizes all global code before the event loop looks into other stacks to execute code.
When a function is marked as async at the beginning, we gain access to a very special keyword called await.
Await can be placed in front of any promise.
You can think of a promise as an object that contains 2 hidden properties: Value and onFulfilled.
As soon as JavaScript execution encounters an await Promise, it uses a web browser API to do something, like fetch some data.
While that data is being retrieved in the background, JavaScript continues executing more global code.
However, since we used the keyword await, there is no more global code.
All the code beneath that await is loaded into a hidden onFulfilled property of the original await promise.
This means that when a value is returned from the web browser API, that onFulfilled code is immediately moved to the microtask queue,
collected by the event loop, placed on the call stack, and executed.
So, in reality, JavaScript maintains its asynchronous nature and is able to execute this asynchronous code one after another.
An easy way to remember this is that the promise executes, the value returns, and all the code beneath it is exposed to the call stack to be executed since that value was fulfilled.
To enhance your interviews, on February 10th, I will be conducting a free class to help software engineers improve their careers.
Send me a message or comment "class" if you want to attend.