Search code examples
javascriptnode.jsasynchronousnode-async

Insert functions via for loop to async


I have the following function:

module.exports = {
  listAccounts: function (callback) {
    var acc = [];
    var calls = [];

    function processAccounts(account, cb) {
      client.getAddressesByAccount(account, function (err, addresses) {
        console.log('getting account', account);
        acc.push({name: account, addresses: addresses});
        return cb();
      });
    }

    client.listAccounts(function (err, res) {
      if (err)
        return callback(err)
      else {
        for (var key in res) {
          calls.push(
            function (cb) {
              processAccounts(key, cb);
            }
          );
        }


        async.parallel(calls, function (err, results) {
          console.log(acc);
        })

      }
    })

  }

}

What I want to do is to use the for loop in order to push all the functions to the calls array and then run async.parallel to run all the functions.

The problem is, that the calls array looks like this after the for loop finishes:

function (cb) {
              processAccounts(key, cb);
            },function (cb) {
              processAccounts(key, cb);
            },function (cb) {
              processAccounts(key, cb);
            }

It does not translate the key to the actual value but keeps it as key


Solution

  • Use function to create a new scope where the value can be container in the closure.

    var res = {a:'hello', b:'howdy'};
    var calls = [];
    
    for (var key in res) {
        calls.push(
            (function(k) {
                return function (cb) {
                    console.log('say: ' + res[k] + ' to ' + cb);
                }
            })(key)
        );
    }
    
    calls[0]('you');
    calls[1]('me');
    

    prints:

    say: hello to you

    say: howdy to me