简体   繁体   English

如何等到2个$ http请求以angularjs结尾

[英]How to wait until 2 $http requests end in angularjs

I need to process responses of two different $http requests. 我需要处理两个不同的$ http请求的响应。 What is the best way to do so, knowing that I have to wait for answers of both request before to process their results. 这样做的最好方法是什么,因为我必须等待两个请求的答案才能处理它们的结果。 I think I must use something like async, promise, await features, but I cannot figure out how to do so. 我认为我必须使用异步,承诺,等待功能之类的东西,但是我不知道该怎么做。

var app = angular.module('Async', []);
app.controller('async', function($scope, $http, $timeout, $interval) {

    $scope.getCamionTypes = function() {
        $http.get("../services/getCamionTypes.php")
        .then(function mySucces(response) {
            $scope.camionTypes = response.data;
        }, function myError(response) {
            camionTypes = [];
        });
    } ;

    $scope.getParametres = function() {
        var b = $http.get("../services/getParametres.php")
        .then(function mySucces(response) {
            $scope.parametres = response.data;
        }, function myError(response) {
            $scope.parametres = [];
        });
    }

    //I make here the first call
    $scope.getCamionTypes();

    //I make here the second call
    $scope.getParametres();

    //The following instruction must wait for the end of the 2 calls
    console.log('Types de camion : ' + $scope.camionTypes + '\n' + 'Parametres : ' + $scope.parametres);

})

Use Promise.all for such use cases. 将Promise.all用于此类用例。 Promise.all takes an array of promises and gets resolved when both the promises are resolved. Promise.all接受一系列的诺言,并在两个诺言都得到解决时得到解决。 It will fail if any of the promises fail. 如果任何承诺失败,它将失败。

$scope.getCamionTypes = function() {
    return $http.get("../services/getCamionTypes.php")
} ;

$scope.getParametres = function() {
    return $http.get("../services/getParametres.php")
}

Promise.all([$scope.getCamionTypes(), $scope.getParametres()]).then(resp => {
//resp[0] and resp[1] will contain data from two calls.
//do all your stuff here
}).catch()

check this 检查这个

let promise1 = $http.get("../services/getParametres.php");
let promise2 = $http.get("../services/getParametres.php");

$q.all([promise1, promise2]).then(result=>{
 //console.log('Both promises have resolved', result);
})

Thank you very much Samira and Shubham for your usefull help ! 非常感谢Samira和Shubham的有用帮助! Here the code modified thank to your advises with the less impact on the architecture of my application. 在这里,修改后的代码感谢您的建议,对我的应用程序体系结构的影响较小。 Best regards. 最好的祝福。

var app = angular.module('Async', []);
app.controller('async', function($scope, $http, $timeout, $interval, $q) {

    $scope.getCamionTypes = function() {
        let promise = $http.get("https://www.alphox.fr/ModulPierres_dev/services/getCamionTypes.php")
        promise.then(function mySucces(response) {
            $scope.camionTypes = response.data;
        }, function myError(response) {
            camionTypes = [];
        });
        return promise;
    } ;

    $scope.getParametres = function() {
        let promise = $http.get("https://www.alphox.fr/ModulPierres_dev/services/getParametres.php")
        promise.then(function mySucces(response) {
            $scope.parametres = response.data;
        }, function myError(response) {
            $scope.parametres = [];
        });
        return promise;
    }

    //I make here the first call
    let promise1 = $scope.getCamionTypes();

    //I make here the second call
    let promise2 = $scope.getParametres();


    $q.all([promise1, promise2]).then (result=>{
        //The following instructions wait for the end of the 2 calls
        console.log('Types de camion : '); 
        console.log($scope.camionTypes);
        console.log('Parametres : ');
        console.log($scope.parametres);
        $scope.both = {camionTypes: $scope.camionTypes, parametres: $scope.parametres}
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM