Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

const b = () => {
  return new Promise(resolve => {
    resolve();
    Promise.resolve()
      .then(() => {
        console.log(1);
      })
      .then(() => console.log(3));
  });
};

const a = async () => {
  await b();
  console.log(2);
};

a();

Different behavior on safari , chrome(firefox), any standard described about this ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
589 views
Welcome To Ask or Share your Answers For Others

1 Answer

You have two completely independent promise chains here:

Promise.resolve()
  .then(() => {
    console.log(1);
  })
  .then(() => console.log(3));


(async () => {
  await new Promise(resolve => {
    resolve();
  });
  console.log(2);
}());

There is no guaranteed ordering other that 3 happens after 1. The rest is affected by how promise callbacks are queued exactly, and there was a change in the spec of await (omitting one unnecessary thenable resolution procedure) that is probably not yet implemented in the Safari engine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...