我有以下功能:
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);
})
}
})
}
}
我想要做的是使用 for 循环将所有函数推送到calls
数组中,然后运行async.parallel
以运行所有函数。
问题是,在 for 循环完成后,调用数组看起来像这样:
function (cb) {
processAccounts(key, cb);
},function (cb) {
processAccounts(key, cb);
},function (cb) {
processAccounts(key, cb);
}
它不会将其转换key
为实际值,而是将其保留为key