如何编写嵌套for循环的async.series

时间:2014-06-11 10:12:25

标签: node.js livescript

我的代码有效:

require! [async]

action = for let m from 1 to 12
    (p) ->
        p null, m

err, data <- async.series action    
console.log data

但我很难让代码在嵌套循环上运行:

action = for let m from 1 to 12
    for let d from 1 to 12
        (p) ->
            p null, (m + "-" + d)

err, data <- async.series action
console.log data

错误消息:

fn(function (err) {
^
TypeError: object is not a function

根据评论的要求,编译的js代码由Livescript生成:

  var async, action, res$, i$;
  async = require('async');
  res$ = [];
  for (i$ = 1; i$ <= 12; ++i$) {
    res$.push((fn$.call(this, i$)));
  }
  action = res$;
  async.series(action, function(err, data){
    return console.log(data);
  });
  function fn$(m){
    var i$, results$ = [];
    for (i$ = 1; i$ <= 12; ++i$) {
      results$.push((fn$.call(this, i$)));
    }
    return results$;
    function fn$(d){
      return function(p){
        return p(null, m + "-" + d);
      };
    }
  }

1 个答案:

答案 0 :(得分:2)

你为嵌套循环得到的action可能是一个嵌套的闭包数组,比如[[fn, fn], [fn, fn]]

所以你想通过连接来展平它们:

err, data <- async.series action.reduce (++)