异步中的递归。电话 - AngularJS

时间:2016-04-21 14:27:46

标签: javascript angularjs

我尝试从递归异步调用创建JSON输出(树结构),并在下面提出 -

$scope.processTree = function (mData, callback) {               
            _processTree.getWebCollection(mData.url).then(
                function(_rdata){
                       // transform xml -> json             
                        var x2js = new X2JS();
                        var json = x2js.xml_str2json(_rdata);
                        //console.log("XML DATA: " + _rdata);
                        //console.log("JSON DATA: " + JSON.stringify(json));
                        var _webs = json.Envelope.Body.GetWebCollectionResponse.GetWebCollectionResult.Webs.Web;

                        // if response has [] of webs - array of objects / sites
                        if ($(_webs).length > 0 && $.isArray(_webs)) {                                  
                            $.each(_webs, function (key) {
                                // loop and build tree
                                mData.children.push({                                           
                                    name: "Site: " + _webs[key]._Title,
                                    url: _webs[key]._Url,
                                    children: []
                                });                                     
                                // recursive loop call for each site again
                                    $scope.processTree(mData.children[key]);                                        
                            });                             
                        }
                        // if response has {} of webs - single object / site                    
                        else if ($.isPlainObject(_webs)) {                                  
                                mData.children.push({                                           
                                    name: _webs._Title,
                                    url: _webs._Url,
                                    children: []
                                });                             
                        }
                        // if no response or response is null, do nothing
                        else {  

                        }   
                }, function(msg){                       
                    alert("ERROR!!! \n\nERROR DATA: " + msg[0] + " \tStatus: " + msg[1]);
                });                 
    };

function callback(mData){
  // do something - use mData, create tree html and display
}

递归获取所有站点和子站点,如果每个站点都存在并存储在变量中--mData,完成后,我需要返回并使用此变量作为JSON输入来创建树图。每个异步。 call返回网站数组或单个网站(如果有)。

如何在整个递归完成后返回mData?如何知道递归是否已经结束并且可以调用所需函数?

1 个答案:

答案 0 :(得分:0)

您可以使用promises和angular的$q服务来使用类似以下的模式,假设foo.getStuff(url)返回角度承诺。

function getRec(url) {
    return foo.getStuff(url).then(function(data){
        var result = {} // construct your result
        var promises = [] // array of promises for recursive calls. 
        for (x in data) {
            promises.push(getRec(url).then(function(r){
                // update result with r
            }))
        }
        // wait for all recursive calls and then resolve the promise with the constructed result
        return $q.all(promises).then(function(){return result}) 
    })
}

getRec(ROOT_URL).then(callback)