I have the following code (http://jsfiddle.net/tj1eo86s/):
promise(1).then(function() {
console.log("OK1");
promise(2).then(function() {
console.log("OK2");
promise(3).then(function() {
console.log("OK3");
}, function() {
console.log("KO3");
});
}, function() {
console.log("KO2");
});
}, function() {
console.log("KO1");
});
If promise(2) is rejected, the output will be:
OK1
KO2
How can I obtain the same behavior while chaining the promises?
I tried with (http://jsfiddle.net/7goh0ds9/):
promise(1)
.then(function() {
console.log("OK1");
return promise(2);
}, function() {
console.log("KO1");
})
.then(function() {
console.log("OK2");
return promise(3);
}, function() {
console.log("KO2");
})
.then(function() {
console.log("OK3");
}, function() {
console.log("KO3");
});
But the output is:
OK1
KO2
OK3
Then I tried to throw an error in the error callbacks (http://jsfiddle.net/cyx6mohy/):
promise(1)
.then(function() {
console.log("OK1");
return promise(2);
}, function() {
console.log("KO1");
throw "KO1";
})
.then(function() {
console.log("OK2");
return promise(3);
}, function() {
console.log("KO2");
throw "KO2";
})
.then(function() {
console.log("OK3");
}, function() {
console.log("KO3");
throw "KO3";
});
But now I have:
OK1
KO2
KO3
I actually understand all those behaviors, but I have not been able to find a solution to handle errors as if promises were nested.