Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS$http.get异步执行顺序_Javascript_Json_Angularjs_Angularjs Http_Hl7 Fhir - Fatal编程技术网

Javascript AngularJS$http.get异步执行顺序

Javascript AngularJS$http.get异步执行顺序,javascript,json,angularjs,angularjs-http,hl7-fhir,Javascript,Json,Angularjs,Angularjs Http,Hl7 Fhir,我最近用AngularJS编写了很多代码。过了一段时间,它开始感到舒适,也变得非常有成效。但不幸的是,有一件事我不明白: 在我的项目中,我需要通过$http.get和RESTful API服务器获取数据。这是我第一次跌倒的地方。在实现了promise($q.defer等)和.then)之后,我认为我解决了这个问题,因为这些函数正在处理需要继续的数据 但在这部法典中: $scope.getObservationsByLocations = function() { var prom

我最近用AngularJS编写了很多代码。过了一段时间,它开始感到舒适,也变得非常有成效。但不幸的是,有一件事我不明白:

在我的项目中,我需要通过$http.get和RESTful API服务器获取数据。这是我第一次跌倒的地方。在实现了promise($q.defer等)和.then)之后,我认为我解决了这个问题,因为这些函数正在处理需要继续的数据

但在这部法典中:

$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
};
我不明白命令的执行顺序。由于我使用Webstorm IDE及其调试功能,所以更准确地说,我不知道为什么命令会按我不理解的顺序执行

简单地说,forEach中包含的所有内容都必须在返回之前执行,因为$http.get是通过.then连接的。但是在调试信息之后,函数会在locationCount++上迭代,甚至在深入之前返回承诺(意思是在第一个.then()之后)

这是怎么回事?我是否误解了AngularJS概念的这一部分

或者这真的是一种糟糕的做法,我应该寻求不同的解决方案


如果上下文很重要/有趣:对象是基于的,即$http.get是异步的,因此根据(除其他外)获取的数据的大小,“完成”get所需的时间是可变的。因此,为什么没有人知道它们将以什么顺序使用JavaScript完成呢?你只能创建单线程应用程序,尽管他们说不一定会这样

但是我们讨论的是真实世界和真实浏览器,因此您的代码示例是作为单个线程运行的(顺便说一句,相同的线程也用于呈现CSS和HTML,至少在Firefox中是这样)

当涉及到异步调用时

$http.get($scope.api + 'Device?_format=json', { 
上面写着“嘿,我以后再做”。它会等待,因为它必须继续当前线程

然后,使用
return
完成当前任务后,它最终可以开始获取远程数据

证据?选中此项:

console.log(1);

对于(var i=0;它们应该同时启动,但是,因为它是异步的,所以不能保证它们将以什么顺序完成。好的,我理解。但是没有办法控制行为或者至少让函数等待吗?我想。然后()(包括promise)同步$http.get!?不可能,除非您希望以串行方式运行它们,这将慢得多。不过,有一些方法可以等到它们全部完成,然后按照发送的顺序处理响应。
console.log(1);

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

console.log(3);