additional syntax (async/await) just means that function can wait for certain time, and is useful for loading files and/or data
Examples:
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function start() {
console.log("Execution...");
for (let i = 0; i < 10; i++) {
console.log(`${i} seconds passed`);
await sleep(1000);
}
console.log("End of execution");
}
console.log("Preparing the program")
sleep(1000).then(start); // you are allowed to call async functions without await
// or without sleep
// start()
Issue:
current stack overflow
sleep
function uses up 100% of CPU time to do it's jobSolution:
Use setTimeout function instead
while this is a viable solution, it has unwanted side-effects
better approach can be found there
additional syntax (
async
/await
) just means that function can wait for certain time, and is useful for loading files and/or dataExamples: