Currently, I am developing a scenario where I use flatMap
alongside Promise.all
. Within the flatMap
function, there are two specific conditions to consider: firstly, checking if the state of the originalObj
is false or not before proceeding with the insertRealStartTime
, and secondly, regardless of this condition, implementing the Event.updateMany
for each event. Additionally, I need to ensure that the return value of Events.updateMany()
indicates the success of the update operation.
function async foo(){
const eventPromises = events.flatMap(async (event) => {
if (event.state.matchPhase == "1H") {
// simpleQuery is async function
const originalObj = await simpleQuery({ id: event.id });
if (originalObj.state == false) {
// insertRealStartTime is async function
await insertRealStartTime(event.id);
}
}
// Event.updateMany is async function
await Events.updateMany(
{ ...event, expireAt: event["startTime"] },
);
}
);
const all = await Promise.all(eventPromises);
console.log(all)
}
Upon executing the code, I noticed that the output in console.log(all)
displays an array of undefined
. It appears that the misuse of await
within flatMap
might be causing this issue, as it interferes with the purpose of using Promise.all
. How can I properly handle these conditional statements in order to execute the asynchronous functions based on the conditions while utilizing Promise.all
effectively?