I need to make 2 AJAX calls, with the second call dependent on the result of the first one. Currently, I am handling it in this way:
Service.getA(car).then(function(carInfo) {
if (carInfo.success) {
Service.getB(carInfo.number).then(function(holderInfo) {
console.log(holderInfo);
});
}
});
Service:
getA: function(car) {
return Server.execute({
method: 'GET',
url: 'a/a',
params: {
car: car
},
}).then(function (carInfo) {
return carInfo;
});
},
The getB
method is similar - just a different URL and parameters. I am new to Angular and would like to refactor this code using promises and defers (as Google suggests that the code will be more elegant). How can I achieve that?