I need to write a function that returns a promise, where first I call a synchronous function A() which returns some result. Then return a function B(result) where B is a promise which takes in the result of A(). If either function fails I want the same error function C(error) to get called where C is a promise. What is the best way of writing this. This is what I have but think there is obvious way I am missing
function() {
try {
var result = A();
return B(result)
.catch(function(error) {
return C(error);
});
}
catch(error) {
return C(error);
}
}
It seems wrong combining synchronous try and catch with a promise .catch and also wrong there are two different places I need to call C(error).
A() throws an error rather than returning an error code.
A()? DoesAreturn an error code that would indicate failure? Does it throw an exception? What type of failure are you trying to detect? Please be more specific so we can offer the best answer. Also, you say you always waysC(error)called, but C is a promise. That statement does make sense. You don't execute a promise like that. What exactly do you mean by that. Do you mean thatCis a function you want called if there's an error? - jfriend00