Consider the following:
const promise = new Promise((resolve) => {
const res = 31 + 42;
resolve(res);
})
promise.then( res => console.log(res));
I understand that as soon as resolve
callback is called, this callback is pushed to the event queue to be executed later on Javascript's call stack.
But what about this line:
const res = 31 + 42;
Where is this line executed? It can't happen synchronously when the Promise
is being created because control returns back immediately without executing the Promise
constructor (which is later executed and might do some heavy calculations). It can't happen even in the Web API
environment because the Web API
environment doesn't have a javascript engine to execute js code. So, where is this line executed?