Why the subsequent code is executed at the same time as the function is called?

Function loadingScreen and the subsequent code are in the same time and take turns. I just recently started with js and programming in General and very hard to deal with it. I think this is due to the fact that the function loadingScreen() async, but I can't make it not async, as otherwise the function sleep() will not work, and it's the only way to stop the script completely. So here's how to make sure that the code worked alternately. Thank you.

function sleep(ms) {
 return new Promise(resolve => setTimeout(resolve, ms));
}

async function loadingScreen() {

 //Javascript code

 await sleep(1000);

}

function game() {

loadingScreen();

 //code to be executed only after the loadingScreen(), but runs at the same time

}
June 27th 19 at 15:04
1 answer
June 27th 19 at 15:06
Solution
the function loadingScreen() implicitly returns a promise, we should wait for its execution

async function game() {
 await loadingScreen()
 console.log('screen is loaded!')
}
Then you have to make the function of the game is also async, but in principle it works, thanks! - nikita.Stracke commented on June 27th 19 at 15:09
:
well, Yes, you're right) having always await requires async
please! - Dianna.Harris commented on June 27th 19 at 15:12
: corrected just in case - Dianna.Harris commented on June 27th 19 at 15:15

Find more questions by tags Asynchronous programmingJavaScript