Middle+
Why does an async function return a Promise if called without await?
sobes.tech AI
Answer from AI
An async function in JavaScript always returns a Promise, even if called without await, because the async function essentially wraps the return value in a Promise. This allows working with asynchronous code uniformly.
For example:
async function foo() {
return 42;
}
const result = foo();
console.log(result); // Promise {<fulfilled>: 42}
Here, foo() returns a Promise that resolves to the value 42. If called with await, you can get the value directly:
async function bar() {
const value = await foo();
console.log(value); // 42
}
bar();
Thus, async functions simplify working with asynchronous code, guaranteeing that the result will always be a Promise.