I have some code using javascript async/await:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function fun1()
{
console.log("dosomething1");
await sleep(6000);
console.log("dosomething2");
return "returnfromfun1";
}
console.log(fun1());
console.log("hello");
According to the official document about async/await:
An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise's resolution, and then resumes the async function's execution and returns the resolved value.
I expect the following output:
dosomething1
//wait for 6 seconds
dosomething2
Promise { <state>: "fulfilled", <value>: "returnfromfun1" }
hello
But the actual output is:
dosomething1
Promise { <state>: "pending" }
hello
//wait for 6 seconds
dosomething2
It seems fun1 returns at the "await" line. Did I misunderstand the description in the official document? And it seems I never get the return value of fun1("returnfromfun1").