简体   繁体   English

动态推送到$ q.all()promise数组

[英]Dynamic pushing to $q.all() promise array

Basically I am trying to create a promise queue in angular that works kind of like $q.all() does, except that it allows me to push more promises onto the array after $q.all() has been executed for example. 基本上我试图在角度创建一个类似于$q.all()的promise队列,除了它允许我在执行$q.all() 之后将更多的promise推送到数组上。

To try and explain better, imagine giving a function a single promise for which you want to execute some code for then() . 为了更好地解释,想象一下函数是一个单一的promise,你想为then()执行一些代码。 However while you are waiting for this first promise to resolve, you give a second promise to the function. 但是,当您等待第一个承诺解决时,您将对该函数提供第二个承诺。 Now I don't want then() being called until both have completed (similar to $q.all() ). 现在我不希望then()被调用,直到两者都完成(类似于$q.all() )。 If the first promise completes before the second is provided, the then() function would be called twice. 如果第一个promise在提供第二个promise之前完成,则then()函数将被调用两次。 This should work for an arbitrary number of promises. 这适用于任意数量的承诺。

Essentially you end up with a promise queue, that executes then() once for every time the queue has been emptied (ie all currently provided promises have been resolved). 基本上你最终会得到一个promise队列,每当队列被清空时执行then()一次(即所有当前提供的promises都已解决)。 I'm sure I could figure out a nice way to do this, just seeing if anyone in SO town has already implemented something, or there is an easy option I am missing. 我相信我能找到一个很好的方法来做到这一点,只是看看SO镇里的人是否已经实现了某些东西,或者我有一个简单的选择。

I faced something similar, perhaps it would be helpful. 我面对类似的事情,也许会有所帮助。

I needed to work through a tree via AJAX calls to build an object that represented the whole tree. 我需要通过AJAX调用来完成一个树来构建一个代表整个树的对象。 When I made the AJAX call it would give me data about the child nodes which I then had to make AJAX calls to get their data. 当我进行AJAX调用时,它会给我关于子节点的数据,然后我必须进行AJAX调用以获取它们的数据。 I needed to have the entire tree before I could render it, so I needed to know when I was done. 在我渲染之前我需要拥有整棵树,所以我需要知道什么时候完成。

The issue with $q.all is that yes you can pass it an array of promises, but the array is fixed at that point even if you add more promises to the array later. $ q.all的问题是,你可以传递一个promises数组,但是即使你稍后向数组中添加更多的promises,数组也会被修复。

My (admittedly hacky) solve was to track the number of unanswered request and redo the $q.all if there were outstanding requests. 我的(实际上是hacky)解决方案是跟踪未答复请求的数量,如果有未完成的请求,则重做$ q.all。

var promisesArr = [];
var unresolved = 0;

function getChildNodes(node) {
   var url = BASE_URL + '/' + node.id;
   var prom = $q(function (resolve, reject) {
            unresolved++;
            $http.get(url).then(function (resp) {
            angular.forEach(resp.data.children, function(v){
               var newNode = {};
               newNode.id = v.id;
               getChildNodes(newNode);
               node.children.push(newNode);
            });
            resolve();
            unresolved--;
            }, function () {
                // Need to handle error here
                console.log('ERROR');
                reject();
                unresolved--;
            })
         });
    promisesArr.push(prom);
}

rootNode = {id: 1}
getChildNodes(rootNode);

allResolved();

    function allResolved() {
        if (unresolved > 0) {
            $q.all(promisesArr).then(allResolved);
        } else {
            RenderTree(rootNode);
        }
    }

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

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