0

Here is the code that I am using:

let myFirstPromise = new Promise((resolve, reject) => {
  setTimeout(function(){
   // alert("sdf");
    resolve("Success!"); // Yay! Everything went well!
  }, 200);
});

console.log(myFirstPromise);

Why does the logged value display like this: enter image description here

but then when I open it, it shows resolved? enter image description here

Another case, Lets say i change the 200 to 2000, now

  1. if i expand before 2 sec, it shows pending always, even if you expand again after 2 min.
  2. if i expand after 2 sec, it shows resolved, and same there after.

Is there a reason it is so ?

Ankur Marwaha
  • 1,613
  • 2
  • 15
  • 30
  • 3
    Hover your mouse over blue "i" icon. – dfsq Mar 30 '17 at 06:34
  • 1
    Because it's not resolved when the `console.log` is executed, and then 200ms it **is** resolved. To see it logged only when resolved, use `myFirstPromise.then(console.log)`. –  Mar 30 '17 at 07:01
  • @torazaburo - that wont log the promise, that will log the resolved value of the promise :p – Jaromanda X Mar 30 '17 at 07:42
  • 1
    The reason your "another case" works as it does is because, well, that's how the console works. –  Mar 30 '17 at 12:58

1 Answers1

1

The first line you see is from the time you return the promise.

Its status is pending at that point (since it is not resolved) and it has no resolution value.

When you open it - the dev tools inspect the live promise - which means it shows the current status. Since you pressed it after more than 200ms - it is now fulfilled with the value "Success!".

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504