执行async.each后如何调用递归函数

时间:2017-03-13 16:15:23

标签: node.js asynchronous recursion

任何人都建议我在执行async.each后如何触发递归函数。我已经通过以下方式完成了逻辑

function recursive(aggs, callback) {
async.each(datas, function(data, asynccallback) { 
    // after some process
    asynccallback(null, true); 
}, function(err, resultData) {
    //after execute the async.each I need to do the following
        if (resultData.flage) { 
            // flage true call recirsive 
            recursive(args, callback); 
        } else { 
            // flage flase means return the callback to called function
            callback(null, data) 
        }
});

}

当我尝试上述方式时,我得到了

  

错误:已经调用了回调。

任何人都可以指导我实现这一目标吗?。谢谢你提前

1 个答案:

答案 0 :(得分:0)

也许尝试Lodash-recursive:

https://www.npmjs.com/package/lodash-recursive

他们为您提供了一个例子:

var assert = require('assert')
var recursive = require('lodash-recursive')

var nodes = [
  {
    value: 'alpha',
    children: [
      {
        value: 'beta'
      }
    ]
  },
  {
    value: 'gamma'
  }
]

var newNodes = recursive.map(nodes, function (node, recursive, map) {
  if (node.children) recursive(node.children)
  return map(node)
}, function (node) {
  if (node.value == 'beta') node.valid = true
  return node
})

var expected = [
  {
    "value": "alpha",
    "children": [
      {
        "value": "beta",
        "valid": true
      }
    ]
  },
  {
    "value": "gamma"
  }
]

assert.deepEqual(expected, newNodes)