'How can I solve the asynchronous problem in my code?

Even though the updating is happening successfully in the DB (collection()....) - the batchIdsAdded data that's supposed to be pushed into packIdsOwned and packsOwnedMetadata arrays in the for loop is displaying as null in the response.

I know there's an asynchronous problem in my code but I'm not sure how to tackle it in my code even after taking a look at this popular SO post revolving around this and trying out some of the suggestions there. Unfortunately, I've hit a wall and I'm very stuck.

How can I make it so that all the batchIdsAdded data is pushed to the arrays correctly?

let packsOwned = result.packsOwned;
let packIdsOwned = [];
let packsOwnedMetadata = [];

let batchIdsAdded = {};

result.packsOwned.push(...batchPackIdInput.map(item => (batchIdsAdded = {"PackId" : item}, collection.updateOne(query, {$push: {"packsOwned" : { $each: [batchIdsAdded]}}}, result['userName']))));  

// Data to be displayed in the response
for(let i = 0; i < packsOwned.length; i++) {
   packIdsOwned.push(packsOwned[i].PackId);
   packsOwnedMetadata.push({
      PackID : packsOwned[i].PackId
   });
}


Solution 1:[1]

I'm not seeing where the async is happening, my best guess is collection.updateOne. If that's the case, then you'll need to Promise.all() the map since at the time of calling it's still a bunch of promises. Unless result is also in an async context tho, it won't matter since everything will be off the stack by the time it goes to push. You'll need to keep everything in an async context after you start one.

async function updateResult() {
  let batchIdsAdded = {};
  return await result.packsOwned.push(Promise.all(...batchPackIdInput.map(
    item => (
      batchIdsAdded = {"PackId" : item},
      collection.updateOne(
        query,
        {$push: {"packsOwned" : { $each: [batchIdsAdded]}}},
        result['userName']
       )
      )
    )
  ));  
}

async function display() {
  let packsOwned = await updateResult().packsOwned;
  let packIdsOwned = [];
  let packsOwnedMetadata = [];
  return packsOwned.forEach(({PackId}) => {
    packIdsOwned.push(PackId)
    packsOwnedMetadata.push({PackID: PackId})
  })
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aaron Rine