I'm new in javascript, coming from firmware / embedded system / RTOS background. I don't understand the concept of the context switching for JS promise.
For this script:
setTimeout(() => { console.log("Hello"); }, 3000);
I can understand that the callback is registered in event loop task, the script continues until finish and exit. The the event loop run and executes any task / callback that is due.
For this script:
var pr = new Promise(function(resolve, reject) {
setTimeout(() => { resolve() }, 3000);
});
pr().then(() => {
console.log("Hello");
});
Does it mean that the script will run pr()
, exit the script, run the event loop. Once event loop executes the callback (calling resolve()
), the JS will switch context to the script again, and run anything inside then()
clause ?
Or the script will run until finish, but the then()
clause is registered as callback in event queue when resolve()
is called. Then the event queue will execute the then()
clause ?
Thank you for your help