Jean
Jean

Reputation: 201

How to wait for promises to be resolved

I have created a function verifyIfUserIsInGame(userId) that allows checking if the user given as argument is in game. This function searches the Firebase database and returns true or false. (so it is a promise within the function).

I would now like to check, once the function has been executed for 2 users if they are in play. I'm not very comfortable with promises. I made this code but it doesn't work since I get [object Promise] in the console logs.

  // We check that both users are in this game
  user1IsInGame = verifyIfUserIsInGame(user1Id); // must return true or false
  user2IsInGame = verifyIfUserIsInGame(user2Id); // must return true or false

  console.log("user1: " + user1IsInGame);
  console.log("user2: " + user2IsInGame);

  if (userIsInGame && user2IsInGame) {
    // The users are in game
  } else {
    // The users are not in game
  }
});

Thanks for your help.

Upvotes: 0

Views: 108

Answers (4)

keja
keja

Reputation: 1363

As already mentioned, you can use Promise.all, but none of the other answers had any content in your verify function, so just wanted to provide a full example.

This is one way of doing it, you can ofc change the resolve to just pass true/false back and then change the if statements.

function verifyIfUserIsInGame(user_id) {
  return new Promise((resolve, reject) => {
    resolve({ id: user_id, inGame: true });
  });
}

// run this when all checks have been performed.
function callbackFunction(players){

  let playersReady = 0;
  players.forEach(player => {
    if(player.inGame){
      console.log(`player ${player.id} are in the game`);
      playersReady++;
    }
  });
  if(playersReady === players.length){
    console.log("All players are in the game");
  }
  
}


// the checks we want to perform
const verifyAllUsers = [
  verifyIfUserIsInGame(1),
  verifyIfUserIsInGame(2)
];

// wait for promises to finish.
Promise.all(verifyAllUsers)
       .then( users => callbackFunction(users) )
       .catch( err => console.error(err) );

Upvotes: 1

atahan
atahan

Reputation: 706

You can use Promise.all if you want to run two functions simultaneously:

  (async function() {
    const usersAreInGame = await Promise.all(verifyIfUserIsInGame(user1Id), 
                                            verifyIfUserIsInGame(user2Id)); 

     if (usersAreInGame[0] && usersAreInGame[1]) {
       // The users are in game
     } else {
       // The users are not in game
     }
 })()

Upvotes: 0

Sebastian Speitel
Sebastian Speitel

Reputation: 7346

You can use async and await to achieve asynchronity.

async function verifyIfUserIsInGame() {
  //the content of your function
  return true;
}


async function func() {
  // We check that both users are in this game
  const user1IsInGame = await verifyIfUserIsInGame(user1Id); // must return true or false
  const user2IsInGame = await verifyIfUserIsInGame(user2Id); // must return true or false

  console.log("user1: " + user1IsInGame);
  console.log("user2: " + user2IsInGame);

  if (userIsInGame && userToKillIsInGame) {
    // The users are in game
  } else {
    // The users are not in game
  }
}

Upvotes: 3

Jonas Wilms
Jonas Wilms

Reputation: 138497

Just await the promises maybe? :

 (async function() {
    const user1IsInGame = await verifyIfUserIsInGame(user1Id); // must return true or false
    const user2IsInGame = await verifyIfUserIsInGame(user2Id); // must return true or false

    console.log("user1: " + user1IsInGame);
    console.log("user2: " + user2IsInGame);

     if (userIsInGame && userToKillIsInGame) {
       // The users are in game
     } else {
       // The users are not in game
     }
 })()

Upvotes: 0

Related Questions