I was wondering what is the problem here, why is the return statement not waiting to finish the previous instruction? orders
are empty, even though I’m logging inside the getOrders
to make sure there were data (there is data).
await stores.forEach(async (store) => {
let arr = await getOrders(store, token, tokenType);
orders.push(arr);
})
return orders;
>Solution :
To wait for all promises, you need an array of promises, which you can get using map. Then you need to use Promise.all, so that it will wait for all the promises in that array to complete.
await Promise.all(stores.map(async (store) => {
let arr = await getOrders(store, token, tokenType);
orders.push(arr);
}));
return orders;