javascript - make async call inside forEach -


i trying iterate thru array of objects , add stuff inside these objects using async function in node.js.

so far code looks like:

var channel = channels.related('channels'); channel.foreach(function (entry) {      knex('albums')         .select(knex.raw('count(id) album_count'))         .where('channel_id', entry.id)         .then(function (terms) {             var count = terms[0].album_count;             entry.attributes["totalalbums"] = count;         });  }); //console.log("i want printed once foreach finished"); //res.json({error: false, status: 200, data: channel}); 

how can achieve such thing in javascript?

use async.each

async.each(channel, function(entry, next) {     knex('albums')          .select(knex.raw('count(id) 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 printed once foreach finished");     res.json({error: false, status: 200, data: channel}); }); 

the final callback called when entries processed.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -