简体   繁体   English

单个承诺后的承诺循环

[英]loop of promises after a single promise

I just have to write a promise, then, with the result of this promise, an array of movies, I have to loop inside the array and for each element do a remote call (with promise too) to decorate with additional data. 我只需要写一个promise,然后在这个promise的结果下,创建一个电影数组,就必须在该数组中循环,并对每个元素进行一个远程调用(也带有promise)以装饰其他数据。 I tried this way: 我这样尝试:

var deferred = $q.defer();
var promises = [];

angular.forEach(tree, function(node) {
    promises.push(
        APICall.GetMovieList(node).then(function(success){
            var contents = {};
            //simple data manipulation
            return contents;
        })
        .then(function(contents){ /////////////////////// concatenation part that won't work
            angular.forEach(contents, function(content) {
                APICall.GetMovieDetails(content.ID).then(function(success){
                    content.metadata = success.metadata;    
                });
            });
            return contents;
        })
    );
});


$q.all(promises).then(ResolveFunction);

return deferred.promise;

For the sake of readability I deleted the ResolveFunction, it simply manage the promise array and do a $q.resolve(results). 出于可读性考虑,我删除了ResolveFunction,它仅管理promise数组并执行$ q.resolve(results)。 The thing is that without the concatenation it works like a charm, but with the necessary concatenation it does just return the same result as before, it behave like this due to async calls that will be called after... Is it a good idea to create another deferred and nest a $q.all inside the concatenation part and return the $q.all solved array promise? 问题是,如果没有串联,它就像一个超级按钮一样工作,但是有了必要的串联,它只会返回与以前相同的结果,由于异步调用将在之后调用,因此它的行为是这样的……这是个好主意吗?创建另一个deferred并在串联部分中嵌套$ q.all并返回$ q.all解决的数组承诺? Thanks so much 非常感谢

You don't need to create a second deferred (you don't need a deferred at all). 您无需创建第二个deferred (根本不需要deferred )。
Simply do the following: 只需执行以下操作:

var promises = [];

angular.forEach(tree, function(node) {
    promises.push(
        APICall.GetMovieList(node).then(function(success){
            var contents = {};
            //simple data manipulation
            return contents;
        })
        .then(function(contents){ 
            var contentPromises = [];
            angular.forEach(contents, function(content) {
                contentPromises.push(
                    APICall.GetMovieDetails(content.ID).then(function(success){
                        content.metadata = success.metadata;    
                    })
                );
            });
            return $q.all(contentPromises).then(function() {
                return contents;
            });
        })
    );
});

return $q.all(promises).then(function(nodes) {
    return nodes;
});

Here is a working jsfiddle-demo with a mocked APICall -Factory. 这里是一个工作的jsfiddle,演示用嘲笑APICall -Factory。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM