I'm doing a admin script in my node backend includes mongo queries/updates and api calls. Im using async and await but it does not work as I want.
(async () => {
// connect to mongo (works ok)
const products = await getProducts(); // Here I have my 10 elements
await Promise.all(products.map(async (prod) => {
response = await getProductInfo(prod.id);
})); // here I call to getProductInfo ten times.
})()
.catch(err => console.error(err));
const getProductInfo = async(idProduct) => {
const response = await rq(optionsProductInfo); //request to API
await Product.updateOne({ sku: response.sku }, { $set: {
priority: response.priority,
price: response.price,
} }); // update product info
};
The problem is that the update in the database is not execute 10 times. Sometimes executed 2, 3, 4 times but never execute for the 10 elements.