在我的 node.js 应用程序中,我有一个代码片段来组成一个 csv 数组:
function process(alerts, callback) {
var csvList = [];
var alertsLength = alerts.length;
alerts.forEach(function(alert) {
var name = alert.name;
var msg = alert.msg;
// retrieve from database
// NOTE: the asynchronous part!
PersonDao.getContact(name, function(error, contact) {
var csv = "csv:" + contact + "|" + msg;
csvList.push(csv);
if (csvList.length == alertsLength) {
// execute callback with the csvList
callback(csvList);
}
});
}); // end of alerts loop
}
有没有更优雅(或更正确)的方法来做到这一点?