To solve the issue using Angular implementation, you can isolate async calls in a separate function that returns a promise, which will be managed automatically.
For example, if you have multiple async calls with authorization checks and want to handle failures gracefully, you can create a function like getCatalogPromise to manage the promises:
function getCatalogPromise(url) {
var deferred = $q.defer();
$http.get(url).then(function (response) {
deferred.resolve(response)
}, function () {
deferred.resolve([]);
})
return deferred.promise;
}
function getAllCatalogs() {
return $q.all([
getCatalogPromise(baseUrl + 'equipment-design/'),
getCatalogPromise(baseUrl + 'engines-design/'),
getCatalogPromise(baseUrl + 'suspension-design/'),
getCatalogPromise(baseUrl + 'artifacts-design/')
]).then(function (data) {
return data;
});
}
The key is to always resolve the promise in getCatalogPromise, regardless of the service response. This ensures that $q.all behaves as expected, allowing you to handle failure cases gracefully by returning an empty array.