JS how to reject wrapper promise from inside one?

Tags: ,



how to reject wrapper promise from inside one or? in other words, how to make number ‘3’ never printed? Current output:

1
2
3

Expected output:

1
2
new Promise(function(resolve, reject) {
  console.log(1)
  resolve()
})
.then(() => console.log(2))
.then(() => { // how to reject this one if internal fails?
  new Promise(function(resolve, reject) {
    reject(new Error('Polling failure'));
  })
  .then(() => console.log(21))
})
.then(() => console.log(3))

Answer

It looks like you’re just missing a return

new Promise(function(resolve, reject) {
    console.log(1)
    resolve()
  })
  .then(() => console.log(2))
  .then(() => { // how to reject this one if internal fails?
    return new Promise(function(resolve, reject) {
        reject(new Error('Polling failure'));
      })
      .then(() => console.log(21))
  })
  .then(() => console.log(3))


Source: stackoverflow