So I have a few functions that all make async calls to a service. I wan't them all to execute after the previous one is complete but only if the previous one didn't fail. It looks something like this:
var fn1 = function() {
var promise = aService.fn1();
var successCallback = function(response) {
return true;
};
var errorCallback = function() {
return false;
};
return promise.then(successCallback, errorCallback);
};
var fn2 = function() {
var promise = aService.fn2();
var successCallback = function(response) {
return true;
};
var errorCallback = function() {
return false;
};
return promise.then(successCallback, errorCallback);
};
var fn3 = function() {
var promise = aService.fn3();
var successCallback = function(response) {
return true;
};
var errorCallback = function() {
return false;
};
return promise.then(successCallback, errorCallback);
};
fn1().then(function(resp){
if (resp)
{
fn2().then(function(resp){
if (resp)
{
fn3().then(function(resp){
if (resp)
{
// all functions have been called in order were successful
}
});
}
});
}
});
The execution at the end looks pretty bad the more function are added to this chain. I wan't to know if there is another way I can structure this in way so it behaves the same but doesn't create a huge tree of chained async calls. If I can keep it on the same indentation that would be great. Thank you!