node.js - Execute callback once every async tasks are finished in a forEach -


i'm writing postgresql transactions, , need execute callback once every function in foreach have been executed. here code :

var sql = "begin;update object set name = "+data.name+", object_subtype_id = "+data.object_subtype_id+" id = "+data.id+";"; db.driver.execquery(sql, function(err, result) {   data.object_subtype.object_property_type.foreach(function(item) {     db.driver.execquery("with upsert (update object_property set value = '"+item.value+"' object_property_type_id = "+item.id+" , object_id = "+data.id+" returning *) insert object_property (object_property_type_id, object_id, value) select "+item.id+", "+data.id+", '"+item.value+"' not exists (select * upsert);", function(err, nb) {       // need send commit; here once functions in foreach have been executed     });   }); }); 

i had @ async i'm not sure how, or if can, apply situation.

have ideas?

thanks!

combining async.series , async.each you'll end beautiful snippet:

var sql = "begin;update object set name = "+data.name+", object_subtype_id = "+data.object_subtype_id+" id = "+data.id+";";  async.series([   function (next) {     db.driver.execquery(sql, next);   },   function (next) {     async.each(data.object_subtype.object_property_type, function (item, next) {       db.driver.execquery("with upsert (update object_property set value = '"+item.value+"' object_property_type_id = "+item.id+" , object_id = "+data.id+" returning *) insert object_property (object_property_type_id, object_id, value) select "+item.id+", "+data.id+", '"+item.value+"' not exists (select * upsert);", next);     }, next);   } ], function (err, results) {   // not sure want result }); 

it might need tweaks depending on want result.


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -