0

Learning JS a little bit and having a problem on a project that I have been working on. I have 2 forEach function that the second one relies on the first one for a variable. I want to await both of them so that the second one only runs once the first forEach has finished. I was hoping for some tips (I know that this can't be the most efficient way of doing this lol

// test data to show structure
const staff = [{
    steamid: 'STEAM_0:1:1111111',
    server: 'testing'}];

const setStaffInfo = async function () {
  try {
    await staff.forEach(async (item) => {
    // this getSteamID64 is a fetch to convert steamID to steamID64
    // this works fine
      const steamid64 = await util.getSteamID64(item.steamid);
      if (steamid64) item.steamid64 = steamid64;
    });
    
    // PROBLEM! I believe this is running before the steamid64 is set in the prior forEach function
    await staff.forEach((el) => {
      if (el.server === "testing" && el.steamid64) {
        el.sits = 5;
      }
    });

  } catch (err) {
    console.log(err);
  }
};
WeLoki1
  • 5
  • 4

0 Answers0