简体   繁体   English

Node.js中的异步和递归

[英]Async and recursion in nodejs

Beginning with express and mongoose i often need to do some batch operations on collections. 快递猫鼬开始,我经常需要对集合进行一些批处理操作。 However it usually involves callbacks which is a pain given how concurrency is coded in nodejs. 但是,它通常涉及回调,考虑到如何在nodejs中编码并发性,这是一个痛苦。 so basically 所以基本上

//given a collection C 
var i = 0;
var doRecursive = function(i){
    if(i<C.length){
      C[i].callAsync(err,result){
        i=+1;
        return doRecursive(i);
       }
    }else{
      return done();
    }
}
doRecursive(i);

Now i dont remember what is the max stack before i get a stackover flow with node , but i guess with 10 000 elements , it wont do. 现在我不记得在获得node的stackover流之前的最大堆栈是多少,但是我猜有1万个元素,它不会做。 I wonder if there are other ways to handle this, if yes , what are they? 我想知道是否还有其他方法可以解决这个问题,如果可以,那是什么? thanks 谢谢

If the goal is to iterate an collection asynchronously, there are numerous control flow libraries available. 如果目标是异步迭代集合,则可以使用许多控制流库。

A good example is async and its reduce function : 一个很好的例子是async及其reduce功能

async.reduce(C, 0, function (memo, item, callback) {
    item.callAsync(function (err, result) {
        if (err) {
            callback(err);
        } else {
            callback(null, memo + result);
        }
    });
}, function (err, result) {
    // ...
});

Note: It's not entirely clear what value you wanted to get from doRecursion , so this just uses addition for an example. 注意:尚不清楚您要从doRecursion获得什么值,因此这里仅以加法为例。

i think you can simply self-iterate instead of true recursion, since you're not drilling into a deep object: 我认为您可以简单地自我迭代而不是真正的递归,因为您不会钻研深层对象:

function doRecursive (C, i){
    i=i||0;
    if(i<C.length){
       C[i].callAsync(err, function(result){
          doRecursive(C, ++i);
       });
    }else{
       done();
    }
};

doRecursive(C);

this does not create a tall stack if the code functions as labeled. 如果代码如标签所示,这不会产生高堆栈。 i localized C so that it executes faster and is potentially re-usable on other collections. 我对C进行了本地化,以使其执行速度更快,并且有可能在其他集合上重用。 the pattern also makes it easy to defer it for long-running operations, just by changing 通过更改模式,还可以轻松地将其推迟用于长时间运行的操作

doRecursive(C, ++i);

to

setTimeout( doRecursive.bind(this, C, ++i), 50 );

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

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