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, r
Solution 1:
It looks like you're just missing a return
newPromise(function(resolve, reject) {
console.log(1)
resolve()
})
.then(() =>console.log(2))
.then(() => { // how to reject this one if internal fails?returnnewPromise(function(resolve, reject) {
reject(newError('Polling failure'));
})
.then(() =>console.log(21))
})
.then(() =>console.log(3))
Solution 2:
In order to reject a promise chain from a .then()
handler, you need to either:
Use throw
Throwing any value will mark the promise as unsuccessful:
const p = newPromise(function(resolve, reject) {
console.log(1)
resolve()
})
.then(() =>console.log(2))
.then(() => { thrownewError(); })
.then(() =>console.log(3));
p
.then(() =>console.log("sucessful finish"))
.catch(() =>console.log("error finish"));
Return a rejected promise
The easiest way is with Promise.reject
:
const p = newPromise(function(resolve, reject) {
console.log(1)
resolve()
})
.then(() =>console.log(2))
.then(() =>Promise.reject("problem"))
.then(() =>console.log(3));
p
.then(() =>console.log("sucessful finish"))
.catch(() =>console.log("error finish"));
Post a Comment for "Js How To Reject Wrapper Promise From Inside One?"