javascript - NodeJs run for each loop parallel -
i'm using each loop fetch data mysql database:
//...sql query returns result var obj = {}; object.assign(obj, result); async.foreach(obj, function (item, callback){ switch(item.type){ case 11: //for example counting likes db.query("select count(*) likes tbl_post_likes binary post_id = ?", [item.post_id], function(err, res){ if(err) throw err; item.likes = res[0].likes; callback(); }); break; case 12: /*i haven't completed other types yet. having similar queries because there different kind of posts. want to run these queries parallel */ callback(); break; case 21: callback(); break; case 22: callback(); break; default: console.log("wtf?: " + result[i]); callback(); break; } }, function(err) { if(err) throw err; res.writehead(200, {"content-type": "application/json"}); console.log(obj); res.end(json.stringify(obj)); });
at end wan't output object "obj" json required informations.
thank & sorry bad english ;)
use async.eachseries instead of async.foreach
following documentation explains in detail,
https://caolan.github.io/async/docs.html#eachseries
more examples here,
Comments
Post a Comment