Why Does a JavaScript Async Function Return a Promise Even When I Return a Number?

Asked 1 hours ago 11 views

0

I’m trying to understand what JavaScript does with the value returned from an async function. Even when the function returns a plain number, the caller receives a Promise:

async function getCount() {
  // The async function wraps this returned value in a Promise.
  return 3;
}

const result = getCount();

// This is false because result is a Promise, not the number 3.
console.log(result === 3);

// Await the Promise to read the fulfilled value.
result.then(count => console.log(count));

Is that wrapping simply part of what the async keyword means, or is there a practical reason for making every return value asynchronous? I’m especially interested in how this affects code that sometimes returns a normal value and sometimes awaits an operation.

0 Answers


Write Your Answer