Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does an async function return a promise even when I return an ordinary value?

Quick answer

An async function always returns a promise. If you write return 7, the caller receives a promise that fulfills with 7, not the number directly. Use await inside another async function to get the fulfilled value, or attach .then() to the promise. For this simple return, an async function is similar to a regular function returning Promise.resolve(7), though the forms are not identical in every detail. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I wrote async function getCount() { return 7; }. Why does getCount() give me a promise instead of the number 7?

Geez

Because an async function always returns a promise. The return 7 supplies the value that the promise fulfills with; it does not make the call itself return a plain number. [S1]

async function getCount() {
  return 7;
}

const result = getCount();

Here, result is a promise. The number 7 is the value it fulfills with. You do not need to write Promise.resolve(7) inside the async function just to wrap that ordinary value; returning 7 already gives the caller a promise fulfilled with it. [S1]

Follow-up question

What does the promise-oriented version look like, and is it exactly the same?

Geez

For this simple example, the async function is similar to a regular function that returns Promise.resolve(7): [S1]

async function getCount() {
  return 7;
}

function getCountWithPromise() {
  return Promise.resolve(7);
}

Both calls give the caller a promise fulfilled with 7. But “similar” does not mean identical in every detail; the documentation cautions against treating these forms as universally identical. [S1] The comparison explains what happens to this ordinary returned value, not that every async function can be replaced without considering its surrounding code.

Follow-up question

How do I get the number itself in the code that calls getCount()?

Geez

Use await inside an async function, or attach .then() to the returned promise. With await, you can use the fulfilled value in the function: [S1]

async function showCount() {
  const count = await getCount();
  console.log(count); // 7
}

Or use a promise handler:

getCount().then((count) => {
  console.log(count); // 7
});

In both cases, getCount() still returns a promise. await and .then() let the caller use its fulfilled value; they do not change what getCount() returns.

Follow-up question

What is the practical before-and-after at the call site?

Geez

Before consuming the promise, the variable holds the result of the call—which is a promise, not the number:

const countPromise = getCount();

To use the number in an async function, await the call:

async function printCount() {
  const count = await getCount();
  console.log(count); // 7
}

Or handle the fulfilled value with .then():

getCount().then((count) => console.log(count));

The change is at the call site: use await or a promise handler when you need the value. In each version, getCount() itself returns a promise; the count used after await or inside the handler is 7. [S1]

A practical checklist

  • If a function is declared with async, treat its call result as a promise.

  • The ordinary value after return is what the returned promise fulfills with.

  • Use await getCount() inside an async function to use the fulfilled value.

  • Use getCount().then(...) to handle the fulfilled value with a promise handler.

  • Do not assume an async function and a regular function returning Promise.resolve(value) are identical in every detail.

A few more useful answers

Does `return 7` mean `getCount()` gives me the number 7?

No. The call returns a promise that fulfills with 7; use await or .then() to consume that value. [S1]

Do I need to write `return Promise.resolve(7)` inside an async function?

Not just to wrap an ordinary value: returning 7 from an async function already makes its returned promise fulfill with that value. [S1]

Are an async function returning 7 and a regular function returning `Promise.resolve(7)` exactly equivalent?

They are similar for this comparison, but not identical in every detail. [S1]

Sources

References used for this guide.

  1. [S1] async function — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work