What i all know about call stack is:
If there is only one function being called it will be pushed to call stack and removed when its job is done or otherwise return statement is encountered.
If one function calls another one, both stays in call stack and popped out in a order; children then parent.
If there is asynchronous code in child function, both are popped out as said in statement 2. but when the asynchronous job is done in future, then child (?) function will be put into Task Queue and then into Call Stack.
But when i used debugger in VSCode:
- I found that both parent and child functions are in call stack after completion of
setTimeout()
. making my belief false of only child being in call stack. - In below code after resolve of promise only const
dummy2 = 'abc'
is available in debugger after completion of asynchronous code, and not the constdummy = 'XYZ'
function asyncfun(){
const innerDummy = '123'
return new Promise((resolve, reject)=>{
setTimeout(()=>{
resolve('You are eligible for voting.');
},5000)
})
}
function outer(){
const dummy = 'XYZ';
asyncfun().then((msg)=>{
const dummy2 = 'abc'
console.log('Promise result:',msg)
})
}
outer();
Note that i had put breakpoints on line:
resolve('You are eligible for voting.');
insidesetTimeOut()
callback, to see who is in call stack after completion/resolve of promise, which happens in 5 seconds in future. Meanwhile when execution reaches to this line,innerDummy
is not available to use.Another breakpoints on line
console.log('Promise result:',msg)
where execution reaches after the above one. Heredummy
is not available to use butdummy2
is.Because of above variables not available for in debugger i am confused whether partial functions are re-pushed to call stack or what?