I'm trying to iterate asynchronously over list of array elements, but with this code:
const calculate = async ({ id, priority, database, product }) => {
let list_of_products;
if (priority) {
list_of_products = await getPriorityList(database, id);
}
else {
const prods = product.split(',');
await prods.forEach((prod) => {
list_of_products = getList(database, prod);
console.log({ list_of_products });
});
}
};
I get the following console.log()
:
{ list_of_products : Promise { <pending> } }
And of course, an error gets thrown eventually.
What is the best practice to handle async calls when iterating? The getList()
method is async as well.