[JavaScript] Return Behavior in JavaScript async Functions
Return in JavaScript async Functions
A point of confusion often arises when returning values from
async functions—especially when writing something like:return Promise.resolve(value);
This looks safe and explicit, but in fact, it's unnecessary.
What async Functions Really Do
In JavaScript, any function declared with the async keyword automatically returns a Promise, regardless of what’s returned inside.
async function getValue() {
return 42;
}
getValue().then(result => {
console.log(result); // 42
});
// Even though the result is returned directly, it's wrapped as
Promise.resolve(42)
When Manual Promise Wrapping Is Needed
The only time Promise.resolve(...) is useful is outside async functions, or when chaining Promises manually.
Comments
Post a Comment