One thread == One call stack == One thing at a time and so we have one call stack.
It is just a memory allocation thing.
Call stack –
It is a data structure which records, where in the program we are. If we step into the function, we push something into the stack and if we return from the function, we pop of from the stack.
Web APIs –
Callback queue –
When you call setTimeOut or any async operation, Web APIs will add them to callback queue. This is also a data structure. It’s work is to store functions in correct order
Event Loop –
This is a constantly running process which checks if call stack is empty or not? If yes then it will execute the function from callback queue. So remember, callback function only get executed if call stack is empty. Always make sure that all callback functions should get chance to enter in call stack.
Event Loop – Example
If you set time to 0 in setTimeOut, it will still run on the last. This time does not stand for time delay after which function will execute, this time is the minimum time function need to wait.
So, this is it for now. Keep doing meaningful code. Keep searching for deep working of any functionality you use.
Share via social media