So I'm somewhat new to asynchronous JavaScript, and I can't figure out why the '.2's are logging before the '.1's.
The only asynchronous method here is makePokemon()
My goal is that all the '.1's log before the '.2's. thanks
sender.room().forEach(async (client) => {
const pokemon = await makePokemon(client.getPokemon());
client.setPokemon(pokemon);
console.log('.1');
});
sender.room().forEach(client => {
console.log('.2');
client.emit('redirect', {
yourPokemon: client.getPokemon(),
theirPokemon: client.getOpponent().getPokemon()
});
});
(client) => { makePokemon(client.getPokemon()).then((pokemon) => { client.setPokemon(pokemon); console.log('1.'); }); }const AsyncAF = require('async-af'); AsyncAF(sender.room()).forEachAF(async (client) => { const pokemon = await makePokemon(client.getPokemon()); client.setPokemon(pokemon); console.log('.1'); console.log('.2'); client.emit('redirect', { yourPokemon: client.getPokemon(), theirPokemon: client.getOpponent().getPokemon() }); });