To effectively tackle this issue, the recommended approach is to utilize the "async" library.
In my view, the flaw in Mr. georgeawg's solution lies in the fact that if $http.get("/ShiftWeb/rest/admin/getallsettingtime")
returns a success response, then $http.get("/ShiftWeb/rest/admin/nextsidor")
will be invoked; otherwise, it won't be executed.
Looking at the question, it appears that both operations are independent of each other.
Hence, it is advisable to adopt a method like async for handling such scenarios.
The revised code snippet would look like this:
var getAllAettingTime = function(cb) {
$http.get("/ShiftWeb/rest/admin/getallsettingtime")
.then(function(response) {
if(response.data){
$scope.settingtimes = response.data;
return cb(null,'OK');
}else{
return cb(null,'ERROR');
})
}
var nextSidor= function(cb) {
$http.get("/ShiftWeb/rest/admin/nextsidor")
.then(function(response) {
if(response.data){
$scope.nextsidor = response.data;
return cb(null,'OK');
}else{
return cb(null,'ERROR');
})
}
async.series([ getAllAettingTime, nextSidor], function(err, result) {
if (err){
/* Handle error conditions here */
} else {
/* Proceed if both HTTP requests succeed */
}
});
In the above async.series()
call, both HTTP requests are initiated independently of one another.
For further insight, it is recommended to delve into the details of the async npm module and incorporate it into your application.