setTimeout / Promise.resolve: Macrotask vs Microtask

You can't control how different architectures queue the promises and timeouts.

Excellent Read Here: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/

If you want the same results you are going to have to chain promises.

let chain = Promise.resolve(null)

for (let i = 0; i < 2; i++) {
  console.log("Chaining ", i);
  chain = chain.then(() => Promise.resolve()
    .then(() => {
      setTimeout(() => {
        console.log("Timeout ", i);

        Promise.resolve()
          .then(() => {
            console.log("Promise 1 ", i);
          })
          .then(() => {
            console.log("Promise 2 ", i);
          })

      }, 0)
    }))
}

chain.then(() => console.log('done'))

This was recognized by the NodeJs team as a bug, more details here: https://github.com/nodejs/node/issues/22257

Meantime it was already fixed and released has part of Node v11.

Best, José