Why is it not waiting?
const wrapper = async () => {
console.log('start');
await setTimeout(async () => console.log('callback'),5000)
console.log('end');
}
wrapper();
Result:
start
end
callback
Expected result:
start
callback
end
You can use a Promise to wait:
const wait = () => new Promise(resolve => {
setTimeout(() => { console.log('callback'); resolve(); }, 5000)
});
const wrapper = async () => {
console.log('start');
await wait();
console.log('end');
}
wrapper();
setTimeout is not async, await will not work as you used it. Wrapping it in a promise which resolves when the timer ends is the right way to do it as await works fine with the promise...
setTimeout()isn't an async function. Forawaitto work the method you call needs to return a promise.