1

I have the code below. I get "callback was already called error on the line after "addToMongo.push(...)". For me that doesn't make a lot of sense since it should only be executed once.

Main Code:

async.waterfall([
    (callback) => {
      sql.query("SELECT id FROM ....", (err, rows, fields) => {
        err ? callback(err, []) : callback(null, rows);
      });
    },
    (rows, callback) => {
        /*Begin for-loop*/
        for (var i = 0; i < rows.length; i++) {
            let id = rows[i].id;

            async.waterfall([
              (callb) => {
                 console.log("doing getvalues");
                 getValues.push(id, (err, values) => {
                    err ? callb(err, []) : callb(null, values);
                 });
              },
              (values, callb) => {
                 console.log("doing addtomongo");
                 addToMongo.push(values, (err, res) => {
                    err ? callb(err, []) : callb(null, res);
                 });
              }
              ],
              (err, res) => {
                 err ? callback(err) : console.log("res");
              });
          }  /*End for-loop*/
     }

    ], (err, result) => {
     err ? console.trace(err) : "";
   });
});

These are the two functions (getvalues and addToMongo)

var getValues = async.queue((id, callback) => {
  let q = query;
  sql.query(q, (err, rows, fields) => {
   err ? callback(err, []) : callback(null, rows);;
  });
}, 5);

var addToMongo = async.queue((values, callback) => {
  callback(null, "done");
}, 5);
trichter87
  • 21
  • 5
  • 1
    There's a really complicated mixture of nested waterfalls and queues happening here. I would suggest you look into something like [co](https://github.com/tj/co) that can vastly simplify your code – Mulan Jun 19 '16 at 19:01
  • Thanks. But it seems co does exactly what async already does. It won't help me much. I will simplify this code but I would like to know what the problem is here. It's a good learning opportunity and I think I might be missing something about async that I need to know. – trichter87 Jun 19 '16 at 19:33
  • 1
    If you're doing async things, it's almost always a mistake to have a synchronous control structure like `for` in your code. If you need to process a set of items, you should be using `async.map` or `async.mapSeries` – loganfsmyth Jun 19 '16 at 19:53
  • 1
    Thank you Logan. I followed your advice and removed the for-loop and used async.each instead to iterate on the rows. Code actually got more concise and it works now. Still not quite happy since I believe it can be done better and it bugs me that I don't know why I got that error. – trichter87 Jun 19 '16 at 20:35
  • @trichter87 nah, the code can be expressed a lot cleaner with co – Mulan Jun 20 '16 at 04:35
  • Just looked into co a little bit. Didn't look into generators much before but looks "promising" (see what I did there? ;) ). – trichter87 Jun 20 '16 at 09:18

0 Answers0