The problem of execution order between promise and setimeout

const promise = new Promise((resolve, reject) => {
    console.log(1);
    resolve();
    console.log(2);
    setTimeout(()=>{
      console.log(7);
    })
});
promise.then(() => {
    console.log(3);
    setTimeout(()=>{
      console.log(6);
    })
});
setTimeout(()=>{
  console.log(5);
})
console.log(4);
Apr.21,2022

setTimeout (fn, 0) is executed at the beginning of the next event loop, and Promise.then () is executed at the end of the current event loop.


if called immediately, promise executes first, then setTimeout executes

Menu