</>
Skip to content
Node.js lessons (30/44)

Node.js — Async Await

Basic Syntax

async function fetchData() {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    return data;
}

Error Handling

async function fetchData() {
    try {
        const response = await fetch('https://api.example.com/data');
        if (!response.ok) throw new Error('HTTP error');
        return await response.json();
    } catch (error) {
        console.error('Fetch failed:', error);
        throw error;
    }
}

Parallel Execution

async function loadData() {
    const [users, posts] = await Promise.all([
        fetchUsers(),
        fetchPosts()
    ]);
    return { users, posts };
}

Top-Level Await

// In ES modules
const data = await import('./module.js');

For Loop

async function processItems(items) {
    for (const item of items) {
        await processItem(item);
    }
}

Mini Practice

  1. Convert promises to async/await
  2. Handle errors with try-catch
  3. Run async functions in parallel
  4. Use async in loops

Up Next

Continue with JSON — working with JSON data.

Related Topics

Frequently Asked Questions about Async Await

What is Async Await in Node.js?

Async Await is a fundamental concept in Node.js. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Async Await?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Async Await.

Why is Async Await important in Node.js?

Async Await is essential for Node.js development. Understanding this concept will help you write better code and solve real-world problems more effectively.