$ http.get上的Javascript回调

时间:2015-08-25 13:45:57

标签: javascript angularjs

我正从不同的json集中检索来自服务器的多个http.get请求。

每个"物理"结束时数据集我执行一个计算,需要来自"虚拟"的值。调用,这是在一个名为"计算"的函数中设置的。 (请参阅下面的代码)

通常,这完全正常并返回正确的值,但偶尔$ http.get请求不会及时完成,因此值仍设置为0.

如何确保每个"群集上的物理和虚拟呼叫的两个 http.get请求都已完成?

function($scope, $http) {
$http.get('http:///search?idc=LH5&type=Virtual&cluster=1').success(function(data) {
    $scope.servers = data; // get data from json
    angular.forEach($scope.servers, function(item) {
        //console.log(item.cores);
        if (parseInt(item.cores) != "NaN" && item.cores != "") {
            if (angular.isNumber(parseInt(item.cores))) {
                tcores1 = parseInt(tcores1) + parseInt(item.cores);
                tmemory1 = parseInt(tmemory1) + parseInt(item.memory);

            }
        }

    })
    console.log("Hall 5. Cluster 1 Total Cores: " + tcores1);
    console.log("Hall 5. Cluster 1 Total Memory: " + tmemory1);


}).then(function() {

    $http.get('http:///search?idc=LH5&type=Virtual&cluster=2').success(function(data) {

        $scope.servers = data; // get data from json
        angular.forEach($scope.servers, function(item) {
            //    console.log(item.cores);
            if (parseInt(item.cores) != "NaN" && item.cores != "") {
                if (angular.isNumber(parseInt(item.cores))) {
                    tcores2 = parseInt(tcores2) + parseInt(item.cores);
                    tmemory2 = parseInt(tmemory2) + parseInt(item.memory);

                }
            }

        })
        console.log("Hall 5. Cluster 2 Total Cores: " + tcores2);
        console.log("Hall 5. Cluster 2 Total Memory: " + tmemory2);


    });
}).then(function() {

    $http.get('http:///search?idc=LH5&type=Virtual&cluster=3').success(function(data) {

        $scope.servers = data; // get data from json
        angular.forEach($scope.servers, function(item) {
            //    console.log(item.cores);
            if (parseInt(item.cores) != "NaN" && item.cores != "") {
                if (angular.isNumber(parseInt(item.cores))) {
                    tcores3 = parseInt(tcores3) + parseInt(item.cores);
                    tmemory3 = parseInt(tmemory3) + parseInt(item.memory);

                }
            }

        })
        console.log("Hall 5. Cluster 3 Total Cores: " + tcores3);
        console.log("Hall 5. Cluster 3 Total memory: " + tmemory3);


    });
}).then(function() {

    $http.get('http:///search?idc=LH5&type=Virtual&cluster=4').success(function(data) {

        $scope.servers = data; // get data from json
        angular.forEach($scope.servers, function(item) {
            //    console.log(item.cores);
            if (parseInt(item.cores) != "NaN" && item.cores != "") {
                if (angular.isNumber(parseInt(item.cores))) {
                    tcores4 = parseInt(tcores4) + parseInt(item.cores);
                    tmemory4 = parseInt(tmemory4) + parseInt(item.memory);

                }
            }

        })
        console.log("Hall 5. Cluster 4 Total Cores: " + tcores4);
        console.log("Hall 5. Cluster 4 Total memory: " + tmemory4);


    });
}).then(function() {

    $http.get('http:///search?idc=LH5&type=Physical&cluster=1').success(function(data) {

        $scope.servers = data; // get data from json
        angular.forEach($scope.servers, function(item) {
            //    console.log(item.cores);
            if (parseInt(item.cores) != "NaN" && item.cores != "") {
                if (angular.isNumber(parseInt(item.cores))) {
                    tpcores1 = parseInt(tpcores1) + parseInt(item.cores);
                    tpmemory1 = parseInt(tpmemory1) + parseInt(item.memory);

                }
            }

        })
        console.log("Hall 5. Cluster 1 Total Physical Cores: " + tpcores1);
        console.log("Hall 5. Cluster 1 Total Physical memory: " + tpmemory1);
        calculate("Cluster 1", "Core", tcores1, tpcores1);
        calculate("Cluster 1", "Memory", tmemory1, tpmemory1);


    });
}).then(function() {

    $http.get('http:///search?idc=LH5&type=Physical&cluster=2').success(function(data) {

        $scope.servers = data; // get data from json
        angular.forEach($scope.servers, function(item) {
            //    console.log(item.cores);
            if (parseInt(item.cores) != "NaN" && item.cores != "") {
                if (angular.isNumber(parseInt(item.cores))) {
                    tpcores2 = parseInt(tpcores2) + parseInt(item.cores);
                    tpcores2 = parseInt(tpcores2) + parseInt(item.cores);

                }
            }

        })
        console.log("Hall 5. Cluster 2 Total Physical Cores: " + tpcores2);
        calculate("Cluster 2", "Core", tcores2, tpcores2);
        calculate("Cluster 2", "Memory", tmemory2, tpmemory2);


    });
}).then(function() {

    $http.get('http:///search?idc=LH5&type=Physical&cluster=3').success(function(data) {

        $scope.servers = data; // get data from json
        angular.forEach($scope.servers, function(item) {
            //    console.log(item.cores);
            if (parseInt(item.cores) != "NaN" && item.cores != "") {
                if (angular.isNumber(parseInt(item.cores))) {
                    tpcores3 = parseInt(tpcores3) + parseInt(item.cores);
                    tpmemory3 = parseInt(tpmemory3) + parseInt(item.memory);

                }
            }

        })
        console.log("Hall 5. Cluster 3 Total Physical Cores: " + tpcores3);
        console.log("Hall 5. Cluster 3 Total Physical memory: " + tpmemory3);
        calculate("Cluster 3", "Core", tcores3, tpcores3);
        calculate("Cluster 3", "Memory", tmemory3, tpmemory3);



    });
}).then(function() {

    $http.get('http:///search?idc=LH5&type=Physical&cluster=4').success(function(data) {

        $scope.servers = data; // get data from json
        angular.forEach($scope.servers, function(item) {
            //    console.log(item.cores);
            if (parseInt(item.cores) != "NaN" && item.cores != "") {
                if (angular.isNumber(parseInt(item.cores))) {
                    tpcores4 = parseInt(tpcores4) + parseInt(item.cores);
                    tpmemory4 = parseInt(tpmemory4) + parseInt(item.memory);

                }
            }

        })
        console.log("Hall 5. Cluster 4 Total Physical Cores: " + tpcores4);
        console.log("Hall 5. Cluster 4 Total Physical Cores: " + tpcores4);
        console.log("Hall 5. Cluster 4 Total Physical memory: " + tpmemory4);
        calculate("Cluster 4", "Core", tcores4, tpcores4);
        calculate("Cluster 4", "Memory", tmemory4, tpmemory4);


    });
});

function calculate(string, type, virtual, physical) {


    console.log(type + " capacity " + string + ": " + (virtual / physical).toFixed(2) * 100 + "%");

}
}]);

3 个答案:

答案 0 :(得分:4)

如果它们可以同时运行,那么立即将它们全部关闭,然后使用$q.all,这将在所有承诺得到解决时解决:

var promiseObj = {};
promiseObj.virtual1 = $http.get('http:///search?idc=LH5&type=Virtual&cluster=1');
promiseObj.virtual2 = $http.get('http:///search?idc=LH5&type=Virtual&cluster=2');
promiseObj.physical1 = $http.get('http:///search?idc=LH5&type=Physical&cluster=1');

$q.all(promiseObj).then(function (results) {
    // results.virtual1 will be the results of virtual / cluster 1 promise
    // results.virtual2 will be the results of virtual / cluster 2
});

答案 1 :(得分:4)

当您的http请求可以并行运行时,您可以注入$ q并使用$q.all(),如下所示:

var https = [
    $http.get(/* .... */),
    $http.get(/* .... */),
    // as many as you wish
];

$q.all(https).then(function(results) {
    console.log('All finished, results:', results);
});

答案 2 :(得分:1)

您没有正确拖动电话。看起来可以并行调用并更新独立状态tcores[1..5]tmemory[1..5]。为此,您可以单独拨打电话,然后创建一个大的承诺,当使用$q.all 所有来解决时,可以解决此问题。

var core1 = $http.get('http:///search?idc=LH5&type=Virtual&cluster=1').success(function(data) {
    $scope.servers = data; // get data from json
    angular.forEach($scope.servers, function(item) {
        //console.log(item.cores);
        if (parseInt(item.cores) != "NaN" && item.cores != "") {
            if (angular.isNumber(parseInt(item.cores))) {
                tcores1 = parseInt(tcores1) + parseInt(item.cores);
                tmemory1 = parseInt(tmemory1) + parseInt(item.memory);

            }
        }

    })
    console.log("Hall 5. Cluster 1 Total Cores: " + tcores1);
    console.log("Hall 5. Cluster 1 Total Memory: " + tmemory1);
});


var core2 = $http.get('http:///search?idc=LH5&type=Virtual&cluster=2').success(function(data) {

        $scope.servers = data; // get data from json
        angular.forEach($scope.servers, function(item) {
            //    console.log(item.cores);
            if (parseInt(item.cores) != "NaN" && item.cores != "") {
                if (angular.isNumber(parseInt(item.cores))) {
                    tcores2 = parseInt(tcores2) + parseInt(item.cores);
                    tmemory2 = parseInt(tmemory2) + parseInt(item.memory);

                }
            }

        })
        console.log("Hall 5. Cluster 2 Total Cores: " + tcores2);
        console.log("Hall 5. Cluster 2 Total Memory: " + tmemory2);
    });

// ...

var allRequestsComplete = false;
$q.all([core1, core2, core3, core4, core5]).then(function () {
     allRequestsComplete = true;
});

function calculate() {
    if (!allRequestsComplete) {
          console.log(type + " capacity " + string + ": " + (virtual / physical).toFixed(2) * 100 + "%");
    } else {
          console.log('Calculation in progress.');
    }
}