I am using Sails, Waterline and Async library
function Outerfunction(listOfProducts) {
var salesOrderId = 1; //some id
var prom = [];
for (var i = 0; i < listOfProducts.length; i++) {
var qty = listOfProducts[i].quantity;
var prod = listOfProducts[i].productName;
var update = function(qty, prod, SalesOrderId) {
CardImages.update({
where: {
productName: prod,
isSold: false,
readyToSell: true
}
}, {
order: SalesOrderId,
isSold: true
})
.exec(function(err, updatedRecords) {
if (err) return err;
return updatedRecords;
});
}
prom.push(update);
}
async.parallel(prom, function(err, result) {
//this callback never gets called
console.log("database calls done");
});
}
I am trying to update database with a for loop, this code works fine and updates the database but but my callback with async.parallel won't get called when all the records are updated.
async.paralleldoes take an array of functions. What doesexecreturn? Given that you've tagged this with [promise], it seems you expect it to return a promise (afaik only if you pass no callback toexec) - so you want to usePromise.all(prom).then(…), and shouldn't use async.js at all!async, purge any notion of promises, change member name "proms" to "tasks", and modify the functions pushed ontotasksto conform with the documentation