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 is a difference between directly writing code in console vs console logging it?

What is a difference between:

Promise.all([1,2]) //Promise {<fulfilled>: Array(2)}

and

let p1 = Promise.all([1,2]);
console.log(p1) //Promise {<pending>}

Why does the console show a different result?

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

>Solution :

Preface: I’m assuming you pasted both lines of your second example (let p1 = Promise.all([1,2]); and console.log(p1)) at the same time; that’s how I can make Chrome’s console do what you’ve described.

Why does the console show a different result?

For no reason that’s important to how you write your own code consuming promises, because it’s impossible for you to do what the console did, which is to synchronously access the state of the promise when building its one-liner synopsis string. The console can do that. Your code cannot (and that’s a good thing). And either way, if you expand the entry in the console for the promise, you’ll see that its state is fulfilled (not pending), because the console shows you the current state as of when you expand the object, not a snapshot.

What’s happening with the console then?

The first example: At the moment the console accessed the promise to build a synopsis string for it, the promise from Promise.all had already had a chance to go from being pending to being fulfilled — that is, apparently the console didn’t access the promise and make that synopsis string for it until after the task queue was empty and microtasks had a chance to run (at which point the promises wrapping the 1 and 2 process their reactions, and the promise from Promise.all gets fulfilled).

The second example: At the moment the console accessed it (during the console.log) in order to build the synopsis string for it, the promise was still pending because the task queue had not yet been empty, so the promise hadn’t had a chance to go from pending to fulfilled, so the string that the console built for it showed pending.

It’s interesting, but it doesn’t really matter. In both cases, the promise from Promise.all is initially pending and then is fulfilled as soon as it can be because what you’ve passed in aren’t pending promises (they’re just non-promise values), and — again — it’s impossible for your code to synchronously access the state of a promise like the console can.

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