I'm currently working with angularjs 1.6.1 and $q. My task involves fetching a large amount of data from an API. I'm struggling to grasp when promises are actually fulfilled. Here's a snippet of what I'm doing:
// controller
this.dataOnePromise = dataOneService.get().then(function (response){
$scope.dataOne = response.data;
return $scope.dataOne;
});
this.dataTwoPromise = dataTwoService.get().then(function (response){
$scope.dataTwo = response.data;
return $scope.dataTwo;
});
let promises = [this.dataOnePromise, this.dataTwoPromise];
$q.all(promises).then(function(response){
// data has been loaded!
// I can now access $scope.dataOne and $scope.dataTwo
});
All the service methods follow a similar structure:
// service. Only the method is shown here
get: function() {
var defered = $q.defer();
$http.get('my api endpoint').then(function(response) {
defered.resolve(response);
}, function(err) {
defered.reject(err)
});
return defered.promise;
}
I'm uncertain if I'm introducing a race condition by not explicitly returning values in the `then` handlers of dataOneService and dataTwoService, potentially causing $q.all(promises) to be fulfilled before the data is assigned to the scope. Can anyone clarify when $q.all(promises) is actually fulfilled? Does it depend on the returns?
Thank you for your assistance.