Chapter XVIII · Async / Await

async

The `async` keyword transforms a standard JavaScript function into a function that implicitly returns a Promise.

01 How It Works

Think of upgrading an ordinary service desk window into an automated express delivery counter.

An async function ensures that any return value is automatically wrapped inside a resolved promise.

example.js
async function getData() {
  return "Hello Async";
}
getData().then(msg => console.log(msg));
  1. 1. Prefix function declaration with the `async` keyword.

  2. 2. Return standard values or throw errors.

  3. 3. Consuming code receives results via `.then()` or `await`.

02 Practical Example

Here is how you might see this concept applied in real-world code:

practical.js
async function getUserID() {
  return 42; // Automatically wraps in Promise.resolve(42)
}

Key Takeaway

The `async` keyword simplifies writing asynchronous code by ensuring functions return promises.

Related Concepts