Skip to content
Advertisement

JS how to reject wrapper promise from inside one?

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))

Advertisement

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))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement