node.jslivescript

How to write async.series nested for loop


I have this code that works:

require! [async]

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

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

but I having difficulties to have the code works on a nested loop:

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

error message:

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

As requested by the comment, the compiled js code, generated by 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);
      };
    }
  }

Solution

  • The action you got for the nested loop is probably a nested arrays of closures, like [[fn, fn], [fn, fn]]

    so you want to flatten them by concatenating:

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