使用递归API的承诺 [英] Recursion using promise api

查看:119
本文介绍了使用递归API的承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在这里找到code

http://plnkr.co/edit/zwCYGQaxyGyr7kL6fLKh?p=$p$ PVIEW

我试图做递归与使用承诺异步功能。我想连续发生它(所以没有$ q.all),并希望等到所有节点都在然后主通话被解雇处理。换句话说,正在退出需要在年底进行打印。我该怎么办呢?

感谢。

程序

  VAR asyncPrint =功能(VAL){
      变种推迟= $ q.defer();      $超时(函数(){
        deferred.resolve(执行console.log(VAL));
        //console.log(val);
      },1000);      返回deferred.promise;
    };
 VAR树= {
      1:{节点:1,CH:[2,3]},
      2:{节点:2,CH:[4]},
      3:{节点:3,CH:[]},
      4:{节点:4,CH:[5]},
      5:{节点:5,CH:[]}
    }   功能复发(TRE,节点){
    返回Async.asyncPrint(节点)
      。然后(函数(){
        变种CH =唱到tre [node.node] TRE [node.node]·CH:[];
        如果(ch.length大于0){
            angular.forEach(CH,功能(D){
                返回复发(TRE,TRE [D]。)
            })
        }
      })
  }  复发(树,树[1]),然后(函数(){
    的console.log('你退出)
  })

输出

 对象{节点:1,CH:数组[2]}
您正在退出
对象{节点:2,CH:数组[1]}
对象{节点:3,CH:数组[0]}
对象{节点:4,CH:数组[1]}
对象{节点:5,CH:数组[0]}


解决方案

 如果(ch.length大于0){
    angular.forEach(CH,功能(D){
        返回复发(TRE,TRE [D]。)
    })
}


这不工作 - 你不必在这里返回一个承诺,当你准备好与复发呼吁,但只返回未定义。事实上,你想用所有这里:

 收益$ q.all(ch.map(功能(D){
    返回复发(TRE,TRE [D]。);
});

或者,按顺序处理 CH 数组:

 函数下一个(我){
    如果(ⅰ&下; ch.length)
        返回复发(TRE,特雷[CH [I]),然后(next.bind(空,I + 1));
    其他
        返回我;
}
返回下一个(0);

Please find the code here

http://plnkr.co/edit/zwCYGQaxyGyr7kL6fLKh?p=preview

I am trying to do recursion with an async function which uses promise. I wanted to happen it serially (so no $q.all), and want to wait till all the nodes are processed before the then of the main call to be fired. In other words, You are exited needs to be printed at the end. How can I do that?

thanks.

program

var asyncPrint = function(val) {
      var deferred = $q.defer();

      $timeout(function() {
        deferred.resolve(console.log(val));
        //console.log(val);
      }, 1000);

      return deferred.promise;
    };


 var tree = {
      1: {node:1, ch: [2,3] },
      2: {node:2, ch: [4] },
      3: {node:3, ch: [] },
      4: {node:4, ch: [5] },
      5: {node:5, ch: [] }
    }

   function recur(tre, node) {
    return Async.asyncPrint(node)
      .then(function () {
        var ch = tre[node.node] ? tre[node.node].ch : []; 
        if(ch.length > 0 ){
            angular.forEach(ch, function (d) {
                return recur(tre, tre[d])
            })
        }
      })
  }

  recur(tree, tree[1]).then(function(){
    console.log('You are exited')
  })

output

Object {node: 1, ch: Array[2]}
You are exited 
Object {node: 2, ch: Array[1]}
Object {node: 3, ch: Array[0]}
Object {node: 4, ch: Array[1]}
Object {node: 5, ch: Array[0]}

解决方案

if(ch.length > 0 ){
    angular.forEach(ch, function (d) {
        return recur(tre, tre[d])
    })
}

That doesn't work - you don't return a promise here for when you are ready with the recur calls but just return undefined. You indeed want to use all here:

return $q.all(ch.map(function(d) {
    return recur(tre, tre[d]);
});

Or, for sequentially processing the ch array:

function next(i) {
    if (i<ch.length)
        return recur(tre, tre[ch[i]]).then(next.bind(null, i+1));
    else
        return i;
}
return next(0);

这篇关于使用递归API的承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆