In my scenario, I have an array called orderCodes
, which stores specific order codes. With each code, I can retrieve the corresponding order details, where each order contains multiple products. My goal is to extract the code of each product from the order details.
The function getOrderDetails()
returns an Observable
containing the results
(an array of products). Each individual result
has a code
property that holds the necessary information.
this.orderCodes.forEach((orderCode) => {
loadOrderDetails(orderCode);
getOrderDetails().subscribe((order: any) => {
if (order.results) {
order.results.map((result) => {
console.log(result.code);
});
}
});
});
I attempted using this forEach
loop, but due to subscribing to an Observable
, the loop moves on to the next iteration before waiting for the previous one to complete. As a result, the desired output is not achieved.
Do you have any suggestions or solutions?