'Waiting for a loop to finish before logging to the console

I'm making a Discord Bot which will hook up to the ROBLOX API via the Bloxy.js Library.

I'm going through the friend list of an user and checking if a friend in their friend list is in one of the 4 groups aforementioned in the code - if it is, it stores the name in an array called "leoUsernames". Once the loop is done, I want it to log "All usernames logged." into the console.

But because I'm new to JS and JS doesn't run like the language I'm used to code in, I'm stuck and I don't know what to use to make it wait for the loop of checking the users to finish before outputting "All usernames logged."

Here is the code:

function CMD_LEOFRIENDS(message,args){
let leoUsernames = new Array();
if(isNaN(args[1])){
    bloxyClient.getIdByUsername(args[1]).then(userId =>{
    bloxyClient.getFriends({
        userId: userId, 
    }).then(friends=>{
        friends.map(totalfriends=>{
            let username = totalfriends.username
            let userId = totalfriends.userId
            bloxyClient.getUserGroups(userId).then(usergroups=>{
                usergroups.map(x=>{
                if(x.group.name == "Mayflower State Police" || x.group.name == "Plymouth Police Department" || x.group.name == "Lander Police Department" || x.group.name == "New Haven County Sheriff's Office"){
                leoUsernames.push(username)
                    console.log("Logging username: " + username)
                }
                }
            )
            })
        });
        console.log("All usernames logged.") })
})
}} 

The expected result is to log all usernames currently being logged to the console. When done logging, log "All usernames logged." to the console.

Actual result



Solution 1:[1]

It is cleaner if you can use async and await than using then. You then get to use normal loops normally.

async function CMD_LEOFRIENDS(message, args) {
  let leoUsernames = [];
  if (isNaN(args[1])) {
    let userId = await bloxyClient.getIdByUsername(args[1]);
    let friends = await bloxyClient.getFriends({
      userId: userId,
    });
    for (let totalfriends of fiends) {
      let username = totalfriends.username;
      let userId = totalfriends.userId;
      let usergroups = await bloxyClient.getUserGroups(userId);
      for (let x of usergroups) {
        if (x.group.name == "Mayflower State Police" || x.group.name == "Plymouth Police Department" || x.group.name == "Lander Police Department" || x.group.name == "New Haven County Sheriff's Office") {
          leoUsernames.push(username);
          console.log("Logging username: " + username);
        }
      }
    }
  }
  console.log("All usernames logged.");
}

Untested but looks like it could work.

Solution 2:[2]

const promise = files.map(async (file) => {
 

})
await Promise.all(promise);

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 Dan D.
Solution 2 Shahnad