Почему после 6 выводится 7, а не 3

почему после 6 выводится 7, а не 3. в начале в консоль выводится поочередно console.lo

Promise.resolve()
    .then(function one() { console.log(1) })
    .then(function two() { console.log(2); throw new Error(); })
    .then(function four() { console.log(4)})
    .catch(function three() {console.log(3)})

Promise.resolve()
    .then(function five() { console.log(5)})
    .then(function six() { console.log(6); throw new Error(); })
    .catch(function seven() {console.log(7)})
    .then(function eight() {console.log(8)})


Ответы (1 шт):

Автор решения: Qwertiy

Потому что не выводится 4. Каждый then или catch создаёт новый промис, который занимает очередь на обработку.

var p = Promise.resolve()
for (let q=1; q<=8; ++q) p = p.then(() => console.log("tick", q))

console.log("(sync)")

Promise.resolve()
  .then() // 1
  .then() // 2
  .then(() => console.log("  first", 3))

Promise.reject()
  .then() // 1
  .then() // 2
  .catch(() => console.log("  second", 3))
  .then() // 4
  .then(() => console.log("  second", 5))

Promise.resolve()
  .then(() => console.log("  third", 1))
  .then() // 2
  .catch() // 3
  .then(() => { console.log("  third", 4); throw 0 })
  .catch(() => console.log("  third", 5))
  .catch() // 6
  .then(() => console.log("  third", 7))
.as-console-wrapper.as-console-wrapper { max-height: 100vh }

→ Ссылка