Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What's called exactly when a promise executor calls the resolve function before you attach a resolve function with 'then'?

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading