I am working on a flatMap
with Promise.all
; there are two condition inside flatMap
, the originalObj
is for checking whether originalObj.state
is false or not, then implement the insertRealStartTime
, and in every event
, I will implement the Event.updateMany
regardless the condition, and I want to return it as well because the return of Events.updateMany()
told me whether or not the update is a success.
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)
}
I got an array of undefined
in console.log(all)
; I think I probably shouldn't use await
inside flatMap
because it lost the meaning of using Promise.all
; however, how should I deal with all these if-statement to make sure the async function will execute base on the if-statement with Promise.all
?