Callbacks, Promises, and async/await

Node.js APIs frequently perform asynchronous operations. Older APIs commonly use callbacks, while modern APIs increasingly return Promises.

async and await provide a readable syntax for consuming Promises. An async function always returns a Promise, and await pauses that function until the Promise settles without blocking the Node.js JavaScript thread.

Async/Await
async function getUser() {
  const response = await fetch('[https://example.com/user');
](https://example.com/user'%29;
)  return response.json();
}

getUser()
  .then(console.log)
  .catch(console.error);
💡 await makes asynchronous code easier to read while the underlying operation remains asynchronous.
Key Takeaways
  • Promises represent eventual completion or failure of asynchronous operations.
  • An async function returns a Promise.
  • Use try/catch around awaited operations when local error handling is required.
Knowledge Check

What does an async function return?