0

console.log(1);
let p = Promise.resolve(2);
p.then(val => console.log(val));
console.log(3);

The above code prints 1 3 2, but since the promise resolving is done synchronously, shouldn't the callback also execute synchronously? Can anyone explain this behavior?

daphtdazz
  • 7,754
  • 34
  • 54
debapam
  • 1
  • 3
  • Promises are async. That's what they're for..I'm not sure what your not understanding? – Liam Mar 27 '18 at 13:08
  • Have a look at https://stackoverflow.com/a/36727148/1823841 & [What is the event loop anyway?](https://www.youtube.com/watch?v=8aGhZQkoFbQ) – palaѕн Mar 27 '18 at 13:09

3 Answers3

2

As per the mozilla developer networks docs:

[Promise] callbacks will never be called before the completion of the current run of the JavaScript event loop.

So your .then() callback is called after the current code has finished.

daphtdazz
  • 7,754
  • 34
  • 54
1

Javascript is single threaded (mostly). To allow JS to scale it relies heavily on asynchronous execution of code. So imagine making a phone call, you ask a question, the person on the other end of the line has to look up the answer, so you wait. All the time your waiting you're not doing anything. This is wasteful of your time.

Now imagine doing the same thing but this time you tell the person on the other end of the line to tell you when they've got the answer. You hang up and do something else. When the other person has finished they call you back to give you the answer to your question. This is a much more efficient use of your time.

In your code the Promise is you making the phone call. The then is the person you've called calling you back when they have the result and your (the person making the call that is) the Js thread.

So you do:

//1
console.log(1);

You then call someone (promise) and ask for them to do some work:

let p = Promise.resolve(2); //nothing printed here

They say they'll call you back:

.then(...) //nothing printed here

You do other work

//3
console.log(3);

They call you back:

//2
val => console.log(val)

1
3
2

but since the promise resolving is done synchronously

No it's not promises are resolved asynchronously

@daphtdazz makes a very good point on why it doesn't just print 2 straight away

console.log is immediate execution. So imagine the person you've just hung up on tries to call you back straight away. But you don't answer yet because you just need to finish this other task. Then you answer.

Liam
  • 27,717
  • 28
  • 128
  • 190
0

The code which is executed in .then callbacks of promise is asynchronous always.

The code inside the promise is not asynchronous.

console.log(1);
let p = new Promise((res, rej) => {
 console.log('sync');
 res(2);
});
p.then(val => console.log('async  ' +val));
console.log(3);
Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
  • This reminds me of [Eisenstein theory of general relativity.](http://www.batesville.k12.in.us/physics/PHYNET/Mechanics/Kinematics/RelSpeed.html) The synchronicity (or lack there of) of this is relative to where your observing it from... – Liam Mar 27 '18 at 13:36
  • @Liam Although there were some famous Eisensteins as well, I am pretty sure you meant Albert **Einstein**, right? – altocumulus Mar 27 '18 at 13:51