使用 AngularJS 的异步调用

Async calls using AngularJS

我们正在使用 Angular 发出多个 HTTP 请求:

$scope.GetTest1 = function () {
    $http.get("/test/GetTest1/").success(function (response) {
        $scope.res = response.aaData;
    });
} 

$scope.GetTest2 = function () {
    $http.get("/test/GetTest2/").success(function (response) { 
        $scope.res = response.aaData; 
    });
}

$scope.GetTest3 = function () {
    $http.get("/test/GetTest3/").success(function (response) { 
        $scope.res = response.aaData; 
    });
}

// This is called from an onclick of a button
$scope.LoadAll = function () {
    $scope.GetTest1();
    $scope.GetTest2();
    $scope.GetTest3();
}

我们假设这些都被称为异步,但是,我们启用了 log4net 并且我们在收到 'gets' 时记录日期时间,所有 3 个的时间是:

19:05:26

19:05:27

19:05:28

这是一个意想不到的惊喜,因为我们假设时间都在 1 秒以内。即异步。

不确定我们是否遗漏了什么,

抱歉,问题是,我们如何进行这些异步调用?

我想这可能是服务器端的原因。当服务器只能处理来自一个客户端的一个请求时,我得到了几乎相同的结果。如果服务器的响应在一秒钟内完成了您的 $http 请求,那么这可能是一个问题。请检查您的网络统计信息,如果您发现它们被同时调用但没有立即得到服务,那么这是服务器端问题。

您可以在浏览器的开发工具时间轴上轻松跟踪此内容