Why this promise return 42?

async function f() {

    return 42; 

}

f().then(console.log);

the output of the function is 42
I have not much experience, but without an await I don’t think you actually have asynchronous code

1 Like

I am not sure, but i think if you use .then function, it will perform as a sync function.
check it by your self.(you don’t need to use await function, if you use then func.)

An async function returns a Promise. You are calling .then() on the Promise and passing console.log as the callback for the onFulfilled handler.

async function f() {
  return 42;
}
// console.log as callback
f().then(console.log);

// the above is the same as explicitly passing the value
f().then((value) => console.log(value));

// Or passing your own callback
function logValue(value) {
  console.log(value);
}

f().then(logValue);
1 Like