Answer the question
In order to leave comments, you need to log in
Why is the following code executed at the same time as the called function?
The loadingScreen function and the following code are executed at the same time, and should take turns. I just recently started js and programming in general and it's really hard to figure it out. I think this is because the loadingScreen() function is async, but I can't make it non-async, otherwise the sleep() function won't work, which is the only way to stop the script completely. So here's how to make the code work in turn. Thank you.
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function loadingScreen() {
//Javascript code
await sleep(1000);
}
function game() {
loadingScreen();
//код который должен выполняться только после окончания loadingScreen(), но выполняется в тоже время
}
Answer the question
In order to leave comments, you need to log in
the function itself loadingScreen()
implicitly returns a promise, you have to wait for its execution
async function game() {
await loadingScreen()
console.log('screen is loaded!')
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question