LOGBOOK

HELP

1 / 23
Other keys: showSpace: good1-4: rate0: skip5: flag

Question

What does the async keyword do when placed before a function?

Answer

The async keyword marks a function as asynchronous, so it always returns a Promise and can use await inside.

The function can use await inside to pause execution until a Promise resolves. Even if you return a plain value, it gets wrapped in a resolved Promise automatically.

async function getData() {
  // Returns Promise.resolve("hello")
  return "hello";
}

Go deeper:

  • doc MDN: async function — why the return value is always wrapped in a Promise and how await works inside.
or press any other key

Question

What does the await keyword do and where can it be used?

Answer

The await keyword pauses an async function until a Promise resolves, then yields its resolved value.

Can only be used inside async functions. Makes asynchronous code read like synchronous code by avoiding .then() chains.

async function fetchUser() {
  const response = await fetch('/api/user');
  const data = await response.text();
  return data;
}
or press any other key