I was attempting to chain two async functions together, because the first had a conditional return parameter that caused the second to either run, or exit the module. However, I've found odd behavior I can't find in the specs.
async function isInLobby() {
//promise.all([chained methods here])
let exit = false;
if (someCondition) exit = true;
}
This is a bastardized snippet of my code (you can see the full scope here), that simply checks if a player if already in a lobby, but that's irrelevant.
Next we have this async function.
async function countPlayer() {
const keyLength = await scardAsync(game);
return keyLength;
}
This function doesn't need to run if exit === true
.
I tried to do
const inLobby = await isInLobby();
This I hoped would await to results, so I can use inLobby
to conditionally run countPlayer
, however I received a typeerror with no specific details.
Why can't you await
an async
function outside of the scope of the function? I know it's a sugar promise, so it must be chained to then
but why is it that in countPlayer
I can await another promise, but outside, I can't await
isInLobby
?
await isInLobby()
, and howinLobby
is used? Also, where/how iscountPlayer
called? – JasonisInLobby().then( … countPlayer().then …
part, the solution is trivial: just make the function in which those calls are contained (the(req, res) =>
one)async
. – Jasonawait
for your code at all? That's why I wondered that you accepted the answer that doesn't really relate to the problem in the question. – Jason