Async Functions Always Return Promises

Async functions always return a Promise: plain values get wrapped, returned promises get adopted, and thrown errors surface as rejections.

JavascriptData Fetching

Code

javascriptsnippet
27 lines
// async 函数永远返回 Promise:普通值被包装,thenable 被收编
async function plainReturn() {
  return 42;
}

async function passThrough() {
  return Promise.resolve('adopted');
}

async function fails() {
  throw new Error('resolver offline');
}

console.log('is promise:', plainReturn() instanceof Promise);

plainReturn().then((value) => console.log('wrapped:', value));
passThrough().then((value) => console.log('adopted:', value));
fails().catch((error) => console.log('rejected:', error.message));

// await 两侧看到的是同一个最终值:包装差异被 await 抹平
async function main() {
  const a = await plainReturn();
  const b = await passThrough();
  console.log('awaited:', a, b);
}

main();

Output

Captured from a sandboxed run of this snippet in the build pipeline.

is promise: true
wrapped: 42
rejected: resolver offline
adopted: adopted
awaited: 42 adopted

How it works

Structurally, expect the functions plainReturn, passThrough, fails and main and nothing else.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. Error is not tied to any input check: it fires whenever execution reaches that line.

Putting it to use

The primary signature is async function plainReturn()

Running the file prints labeled results such as is promise:, wrapped:, adopted: — each operation’s effect is visible in the output.

main is the driver: it invokes plainReturn and passThrough before returning.

Core lines

return Promise.resolve('adopted');
throw new Error('resolver offline');
const a = await plainReturn();

FAQ

What triggers the exception?
No. The `Error` is not behind any guard — it triggers purely on reaching that line of code.
What does Async Functions Always Return Promises print when run?
Invoke the file as-is — the `is promise:`, `wrapped:`, `adopted:` prefixes identify which step printed each line.