I have a specific case where I need to wait for a async calls result before continuing. I am using the async/await keywords, but not having any luck. Any help appreciated.
This is my attempt to try getting it to work, the numbers should be in numerical order.
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function demo() {
document.writeln('2...');
await sleep(2000);
document.writeln('3...');
}
document.writeln('1...');
demo();
document.writeln('4.');
3comes last, because that’s the last thing that gets executed after the Promise function. Do you expect1...2...3...4.to be displayed at the very end? Then don’t usedocument.writeln. Use standard DOM APIs instead.