在 forEach 中进行异步调用

make async call inside forEach

我正在尝试遍历对象数组并使用 Node.js 中的异步函数在这些对象中添加一些内容。

到目前为止,我的代码如下所示:

var channel = channels.related('channels');
channel.forEach(function (entry) {

    knex('albums')
        .select(knex.raw('count(id) as album_count'))
        .where('channel_id', entry.id)
        .then(function (terms) {
            var count = terms[0].album_count;
            entry.attributes["totalAlbums"] = count;
        });

});
//console.log("I want this to be printed once the foreach is finished");
//res.json({error: false, status: 200, data: channel});

我怎样才能在JavaScript中实现这样的事情?

使用async.each

async.each(channel, function(entry, next) {
    knex('albums')
         .select(knex.raw('count(id) as album_count'))
         .where('channel_id', entry.id)
         .then(function (terms) {
            var count = terms[0].album_count;
            entry.attributes["totalAlbums"] = count;
            next();
         });
}, function(err) {
    console.log("I want this to be printed once the foreach is finished");
    res.json({error: false, status: 200, data: channel});
});

处理完所有条目后将调用最终回调。

既然您已经在使用 promises,最好不要将这个比喻与 async 混为一谈。相反,只需等待所有承诺完成:

Promise.all(channel.map(getData))
    .then(function() { console.log("Done"); });

其中 getData 是:

function getData(entry) {
    return knex('albums')
        .select(knex.raw('count(id) as album_count'))
        .where('channel_id', entry.id)
        .then(function (terms) {
            var count = terms[0].album_count;
            entry.attributes["totalAlbums"] = count;
        })
    ;
}