I'm trying to rewrite the code of one my project with the promise (Q). I don't know how to take the result of a chain of promises inside a foreach loop:
var resProducts = [];
products.forEach(currProduct) {
Stock.saveNewStock(currProduct)
.then(function(res1) {
Product.saveNewProduct(currProduct)
.then(function(res2) {
resProducts.push(res2);
Product.addStockToProduct(res1,res2)
})
})
.catch(function(err) {
console.log(err)
})
});
console.log(resProducts);
I see that every time the log stamps an empty array, so how I can resolve it?