Friday, 21 August 2020

How to solve this problem without using setTimeOut?

async function sellerSelection() {let selectedSellerArray = [];await orders.forEach(async (order) => {let orderItems = await orderItemsObj.findMany({where: { order_id: order.id, status: "created" },});orderItems.forEach(async (orderItem) => {const sellerSelect = await _calculateHighProfitSeller(order.id, orderItem);selectedSellerArray.push(sellerSelect.seller_id);        });    });setTimeout(() => {console.log(selectedSellerArray, "selectedSellerArray");    }, 1000);}Hello everybody, I am learning nodejs and dont have a proper grasp of asynchronous stuff in it. So, in the above code if i dont use setTimeOut(), the _calculateHighProfitSeller() wont have returned values fast enough, but the code gets executed anyway so I dont get anything. What I want to do is execute the code inside setTimeOut() only after _calculateHighProfitSeller() calls have returned its values.

Submitted August 21, 2020 at 09:26AM by ajirnanashak

No comments:

Post a Comment