different promise execution on browsers, what happened?

我只是一个虾纸丫 提交于 2020-06-23 04:51:29

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/61234256/different-promise-execution-on-browsers-what-happened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!