0

I'm trying to understand the JS event loop, and

Promise.resolve()
    .then(() => {
        console.log(0)
        return Promise.resolve(4)
    })
    .then(res => {
        console.log(res)
    })
Promise.resolve()
    .then(() => {
        console.log(1)
    })
    .then(() => {
        console.log(2)
    })
    .then(() => {
        console.log(3)
    })
    .then(() => {
        console.log(5)
    })
    .then(() => {
        console.log(6)
    })

Why the result is 0 1 2 3 4 5 6

Why Promise.resolve(4)'s callback always be the 5th

DBS
  • 9,110
  • 4
  • 35
  • 53
Zerok
  • 1
  • 1
  • I don't think you are guaranteed for that. – Daniel A. White Mar 10 '23 at 16:07
  • 1
    Why wouldn't it? – Bergi Mar 10 '23 at 16:13
  • 1
    If I may, your question at https://stackoverflow.com/questions/75693852/about-js-event-loop-why-different-result was showing a very interesting and surprising behavior (though it didn't need the queueMicroTask) and it was close from being reopened after your edit (2 / 3 required votes). It might still need a bit of polishing, but it would be great if you could undelete it. – Kaiido Mar 10 '23 at 23:57
  • 1
    @Kaiido Fancy. I didn't know that one can reopen questions while they are deleted! And just yesterday I learned that it's possible to edit the duplicate list of deleted questions… – Bergi Mar 11 '23 at 00:45

0 Answers0