You're using setTimeOut without creating the promise object, so it's waiting for the setTimeOut value to be returned (which is instant) rather than waiting for a promise resolution. This is the reason your await statement isn't working the way it was intended to. What you need is to create a promise:
function resolveAfterOneSecond(x) { return new Promise(resolve => { setTimeout(() => { console.log("Middle"); resolve(x); }, 1000); }); } async function f1() { var x = await resolveAfterOneSecond(10); console.log("End"); } console.log("Begin"); f1();
and then set your function to await the return of the promise, rather than the return of the setTimeOut function integer.
setTimeoutreturns a number, not a promise. Only if youawaita promise does the execution actually "wait".