Async & Await in JavaScript

JavaScript·2 min read·Jan 1, 2025

The async and await keywords are used to work with asynchronous code in a more synchronous manner. They eliminate the need for complex chaining and nested then() blocks by flattening the flow into a synchronous-looking structure.

They also simplify error handling by centralizing rejected promises in a try...catch block, ultimately making the code more linear and easier to follow.

Declaring asynchronous functions

An asynchronous function is a function that returns a promise and is declared using the async keyword:

async function functionName(parameters?) {  //}

Note: Any value returned from an async function using the return statement will be automatically wrapped in a resolved promise.

Awaiting for promises

When used within an async function, the await keyword allows you to pause the execution of the function and wait until a promise resolves or rejects:

async function functionName(parameters?) {  try {    let result? = await Promise;    // process result  } catch(error) {    // process error

Unlock the program 🚀

Pay once, own it forever.

€79

30-day money-back guarantee

  • 13 modules
  • 113 lessons with full-code examples
  • 29 projects with commented solutions
  • All future lesson and project updates
  • Lifetime access

By submitting this form, you agree to the Terms & Conditions and Privacy Policy.