0

I have this function

const groupList = async (io, socket, userid) => {
try {
  var response = {};
  var data = []
  ddb.get({
    TableName: "TableName",
    Key: { Username: userid }
  }).promise()
  .then(async (user) => {
    const group = user.Item.Chatgroups
    for(let i = 0; i < group.length; i++) {
        ddb.get({
          TableName: "TableName",
          Key: { ChatID: group[i] }
        }).promise()
        .then(async (groupData) => {
          await data.push({
            ChatID: groupData.Item.ChatID,
            Chatname: groupData.Item.Chatname
          })
          response["groups"] = data
        })
    }
  })
} catch (error) {
  
} finally {
  return response
}
};

I am using dynamodb (ddb) and it supports promises. When I call this function I get response as "undefined". However, I used a timeout function after my for loop and it displays the data. So clearly my code is not waiting for the execution of for loop and then returning the "response".

How can I make the code to wait for completing the for loop execution and then return the response.

0 Answers0