I know await
in loops is highly discouraged. But I'm stuck on a specific case I cannot figure out how to do it efficiently. I want a final output of variable values
like this
{
jobId1: [[..], [..], [..], [..]],
jobId2: [[..], [..], [..], [..]] // list goes on
}
The below snippet represent my current implementation.
for (let jb of jobList ) {
const subArray = []
const rel1 = new Parse.Relation(jb, 'applicants')
const rel2 = new Parse.Relation(jb, 'shortlisted')
const rel3 = new Parse.Relation(jb, 'hired')
const rel4 = new Parse.Relation(jb, 'rejected')
subArray.push(rel1.query().containedIn('objectId', uniqUserIds).select('objectId').find())
subArray.push(rel2.query().containedIn('objectId', uniqUserIds).select('objectId').find())
subArray.push(rel3.query().containedIn('objectId', uniqUserIds).select('objectId').find())
subArray.push(rel4.query().containedIn('objectId', uniqUserIds).select('objectId').find())
values[jb.id] = await Promise.all(subArray)
}
I can push all the promises into one single array and wait for all. but I'd lose out the track which promise value belongs to which job id. Though splitting the whole await array by every 4th index will get me what I want, I'm looking for even better alternatives.