Hi, I’m checking out async/await by calling a Promise (with a timer).
Within the async-function it works as expected: [1]
But if I return that value, a Promise-object is logged: [2]
I don’t understand that:
at [1] the code is waiting for the result of the Promise,
but at [2] the Promise is returned immediately.
[2] is executed before [1].
// a Promise
const foo = ()=>{
return new Promise(function(resolve, reject){
window.setTimeout(()=>{resolve("async/await-test");}, 2000);
})
}
// call Promise with async/await
const goo = async() => {
const res = await foo();
// [1] logs: "async/await-test"
console.log(1, res);
// [2] returns Promise-object ???
return res;
}
console.log(2, goo()); // logs: [object Promise]