javascript async/await not working
The async function will return a Promise
, so you need to await the call to demo
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms))
const demo = async() => {
console.log('2...')
await sleep(2000)
console.log('3...')
}
const blah = async() => {
console.log('1...')
await demo()
console.log('4.')
}
blah()
You should use .then()
after async function.
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().then(() => {
document.writeln('4.');
});