I have two promises in chain, like this:
router.get('/:address', function (req, res, next) {
let prodotti = [];
istanza.methods.getProductByOwner("xxxxx").call().then(
ids => {
for (let id of ids) {
istanza.methods.prodotti(id).call().then(
prod => {
prodotti.push(prod);
},
error => console.dir(error)
)
}
},
err => console.log("ritiro KO " + err)
);
res.render('profilo', {address: req.params.address, prodotti: prodotti});
});
I want to have prodotti filled, when the for is finished.
I try this way:
router.get('/:address', function (req, res, next) {
let prodotti = [];
istanza.methods.getProductByOwner("xxxx").call().then(
ids => {
f(ids)
},
err => console.log("ritiro KO " + err)
);
res.render('profilo', {address: req.params.address, prodotti: prodotti});
});
and then
async function f(ids) {
let prodotti = [];
let promises = []
promises.push(new Promise((resolve, reject) => {
for (let id of ids) {
promises.push(istanza.methods.prodotti(id).call().then(
prod => {
prodotti.push(prod);
//resolve when for is finished
if(id == (ids.length)-1){
resolve(prodotti)
}
id++;
},
error => console.dir(error)
));
}
}));
let result = await Promise.all(promises);
console.log("await " + result);
}
but my result is:
,,,[object Object],[object Object],[object Object]
I Want get result only when for is finished, and I don't like to use condition like this:
if(id == (ids.length)-1)