I swear nothing in this language makes sense. The promise executor is run immediately, and in this case it calls the resolve function, except a resolve handler hasn’t been attached yet:
// FLOW STARTS HERE
let promise = new Promise(function(resolve, reject) {
// THIS IS THE EXECUTOR, RUN IMMEDIATELY ON PROMISE CONSTRUCTION
// THE PROMISE CONSTRUCTOR CALLS THIS FUNCTION, WITH WHAT ARGUMENTS?
console.log("inPromise");
resolve(value); // here, what is called?
console.log("logged again"); // this is logged
});
// CONTROL FLOW CONTINUES
promise.then(() => console.log("promise fulfilled"));
console.log("hello");
>Solution :
The resolve function just sets the internal state of the promise to "resolved". Since it’s now resolved, if one or more .then callbacks have already been set up, they will get called. If not, no problem, you just have a promise sitting there in the resolved state. Later on, if you call .then on an already-resolved promise, your callback will get executed.