递归HTTP请求javascript

recursive http request javascript

本文关键字:javascript 请求 HTTP 递归      更新时间:2023-09-26

我正在使用AngularJS制作一个单页应用程序。我想不断有一个待处理的http请求到服务器,所以一旦服务器响应,发送另一个http请求。我编写了递归函数stateCheck(),但是我的代码被卡在函数内部,并且从不返回工厂,也从不更新视图。

我如何让这些http请求在我的应用程序中不断挂起,而不会陷入无限循环?

define([
    'angular',
    'app',
    'angularRoute',
    ], function (angular, app) {
        'use strict';
        app.config(function ( $httpProvider) {        
            /* Angular automatically adds this header to the request,
               preventing us from being able to make requests to the server on another port */
            delete $httpProvider.defaults.headers.common['X-Requested-With'];
        }).factory('equipmentService', ['$http', '$rootScope', function($http, $rootScope) {
            var factory = {
                getEquipmentState: $http.get($rootScope.backendServer+'/equipment/state'),
                setEquipmentState: function(state){
                    this.equipmentState = state.state;
                    console.log('equipment state', this.equipmentState);
                    redirectState(this.equipmentState);
                }
            }
            var redirectState = function (state) {
                switch(state) {
                    case 'Active':
                        $location.path('/active');
                        break;
                    case 'Review':
                        $location.path('/review');
                        break;
                    default:
                        // don't change views
                }
            }
            function stateCheck () {
                factory.getEquipmentState
                .then(function (data) {
                    factory.setEquipmentState(data.data);
                })
                .then(stateCheck);
            }
            stateCheck();
            return factory;
        }]);
});

问题在于初始化factory.getEquipmentState的方式。根据定义,初始化本身已经调用了/GET请求,因此访问stateCheck()中已解析的承诺确认了第一次调用,但不确认下一次调用。

要解决这个问题,你可以这样构造你的equipmentService:

 factory('equipmentService', ['$http', '$rootScope', 
    function($http, $rootScope) {
       var factory = {
          getEquipmentState: function() {
             return this.equipmentState;
          },
          setEquipmentState: function(state){
             this.equipmentState = state.state;
             console.log('equipment state', this.equipmentState);
             redirectState(this.equipmentState);
          }
       };
       var redirectState = function (state) {
          switch(state) {
             case 'Active':
                $location.path('/active');
                break;
             case 'Review':
                $location.path('/review');
             break;
             default:
                // don't change views
          }
       };
       function stateCheck () {
           $http.get($rootScope.backendServer+'/equipment/state')
               .success(function (data) {
                   factory.setEquipmentState(data.data);
               })
               .then(stateCheck);
        }
        stateCheck();
        return factory;
    }])

问题:

你的代码不工作,因为你总是使用相同的承诺对象:

$http.get($rootScope.backendServer+'/equipment/state')已经返回一个承诺,并且只会被解决一次。

解决方案:

只保存具有正确参数的函数,因此更改

getEquipmentState: $http.get($rootScope.backendServer+'/equipment/state'),

getEquipmentState: $http.get.bind($http, $rootScope.backendServer+'/equipment/state'),


stateCheck中调用这个函数。这意味着替换

factory.getEquipmentState
factory.getEquipmentState()


下面是一个工作示例