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,

http://wirama.web.id/node-js-array-looping-with-async/


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -