- Thread of execution and Global execution context
2. Call Stack (Last In First Out)
3. CallBacks, CallBack Queue and Event Loop
In this example we have an asynchronous function called getUserDataFromApi this function will take 1 second to get the data i used setTimout function to simulate the real http request, to get the result i passed a callBack function called ShowUserData takes userdata as an argument and console log it.
Let’s what will happen when when the thread of execution run the function getUserdataFromApi() at line 13 :
when the thread arrive to line 8 getUserdataFromApi() will be pushed into the Call Stack , a brain new execution context will be created, and the callBack function ShowuserData will be pushed into CallBack Queue.
Here we introduce the Event Loop who is responsible for checking if the stack is empty or not. If the call stack is not empty even after 2 sec the ShowUserData will wait more.
The output of our example will be :