无法同时在angularjs中执行2 $ http调用

时间:2015-08-04 16:11:35

标签: javascript ajax angularjs

我正在实现长轮询以了解服务器端某些长时间运行事件的状态。我创建了自己的工厂,它会在服务器事件触发时通知我。这是工厂。

.factory("$httpPolling", function ($http) {

        function $httpPolling($httpService) {

            var _responseListener, _finishListener;
            var cancelCall = false;
            var _pollId;

            function waitForServerCall(id) {
                console.log("executing waitForServerCall");
                    $httpService.get(href("~/polling/" + id))
                        .success(function (response) {
                            var cancelPolling = _responseListener(response);
                            if (cancelPolling || cancelCall) {
                                return;
                            }
                            else {
                                waitForServerCall(id);
                            }
                    });
                };

                function _sendData(httpMethod, url) {

                    var pollingId = guid();
                    _pollId = pollingId;
                    if (url.split("?").length == 2) {
                        url += "&pollid=" + pollingId;
                    }
                    else {
                        url += "?pollid=" + pollingId;
                    }


                    if (httpMethod == 0) {
                        $httpService.get(url).success(function (response) {
                            if (_finishListener) {
                                _finishListener(response);
                            }

                            cancelCall = true;
                        });
                    }
                    else {
                        $httpService.post(url).success(function (response) {
                            if (_finishListener) {
                                _finishListener(response);
                            }

                            cancelCall = true;
                        });
                    }
                }
                var $self = this;

                this.get = function (url) {
                    _sendData(0,url);
                    return $self;
                };

                this.post = function (url) {
                    _sendData(1, url);
                    return $self;
                };

                this.listen = function (_listener) {
                    _responseListener = _listener;
                    waitForServerCall(_pollId);
                    return $self;
                }

                this.finish = function (_finish) {
                    _finishListener = _finish;
                    return $self;
                }

            }

        return new $httpPolling($http);
    });

使用的sintax应该是:

$httpPolling.get("url")
.listen(function(event){
// fires when server event happend
})
.finish(function(response){
// fires when the long running process finish
});

问题是_sendData方法不会异步执行,因为waitForServerCall仅在_sendData(长时间运行的进程)方法从服务器获得响应时才执行ajax调用。 / p>

为什么呢?这是一种角度行为吗?

1 个答案:

答案 0 :(得分:1)

Angular $ httpProvider为异步http调用提供了一个选项,默认值设置为false。 尝试

app.config(function ($httpProvider) {
  $httpProvider.useApplyAsync(true);
});