AngularJS $ http.get asynchronous call order

I recently coded a lot in AngularJS. After some time, he began to feel comfortable with him, and also became really productive. But, unfortunately, there is one thing that I do not understand:

As part of my project, I need to get data through the $ http.get and RESTful API server. Here I started to stumble first. After fulfilling the promise ($ q.defer, etc. Etc.) In the functions that process the data needed to continue, I thought I defeated the problem.

But in this code:

$scope.getObservationsByLocations = function() {
        var promise = $q.defer();
        var locationCount = 0;

        angular.forEach($scope.analysisData, function(loc) {   // for each location
            $http.get($scope.api + 'Device?_format=json', {     // get all devices
                params: {
                    location: loc.location.id
                }
            }).then(function (resultDevices) {
                var data = angular.fromJson(resultDevices);
                promise.resolve(data);
                // for each device in this location
                angular.forEach(angular.fromJson(resultDevices).data.entry.map(function (dev) {
                    http.get($scope.api + 'Observation?_format=json', {     // get all observations
                        params: {
                            device: dev.resource.id
                        }
                    }).then(function (resultObservations) {
                        var observations = angular.fromJson(resultObservations);
                        // for each obervation of that device in this location
                        angular.forEach(observations.data.entry.map(function(obs) {
                            $scope.analysisData[locationCount].observations.push({observation: obs.resource});
                        }));

                    })
                }))
            });
            locationCount++
        });
        return promise.promise
};

I cannot understand in what order the commands are executed. Since I use the Webstorm IDE and its debugging function, it would be more accurate to say that I do not know why the commands are executed in the order that I do not understand.

, , forEach, , , $http.get .then's. locationCount ++ , ( .then()).

? AngularJS?

, ?

/: i.e https://www.hl7.org/fhir/2015May/location.html#5.15.3

+4
2

JavaScript , , , , , .)

, (, CSS HTML, , Firefox).

$http.get($scope.api + 'Device?_format=json', { 

", ". , .

, return, , , .

? fiddle:

console.log(1);

for (var i=0;i<1000000;i++) setTimeout(function(){
    console.log(2);
},0);

console.log(3);

for? , setTimeout. 3 2, , 3.

+2

$http.get , , ( ) , , , , "" get, . , .

+1

All Articles