Hi.
I have a program, that goes pretty much like this:
doA()
.then(doB, rejectHandler)
.then(doC, rejectHanlder)
.then(doD, reject Handler)
rejectHandler()
is just a short function that displays a generic error message to the user, and prints a rejection message (i.e. the string that I pass as and argument for doA, doB, doC and doD
's reject function) to the console. It works, but i would like to improve it, by showing the user which step exactly failed. However, every then()
in the chain is called no matter if the previous promise was fulfilled or rejected. This means, that if my doA()
returns a rejected promise for some reason, all three then()
's will fire, calling rejectHandler()
three times. This means that no matter which promise was initially rejected, my user will only receive a rejection msg for doC
(…right?).
Now I could possibly modify rejectHandler()
so it reacts different to each rejection, but it would be painfully verbose and it wouldn’t work for bigger apps. Also, truth to be told, if any of the do*()
functions returns rejected promise, there is absolutely no point in starting the next step. So is there any way that I could break the then-chain? I.e. if rejectHanlder()
was called, don’t call subsequent then()
's?
On the side note, I’m using fetch API in this program. In order to use it as a fulfillment handler for one of then()
's, I had to wrap it into function. A simple thing like this works:
function fetchWrapper(url){
return fetch(url);
}
But I wanted to give it some custom rejection message. So far the best way I found is this:
function fetchWrapper(url){
return fetch(url)
.then(function(data){
return Promise.resolve(data);
},
function(){
return Promise.reject("My custom message");
});
}
It works, but looks really clumsy. Any better ways to do it?
Thanks in advance.