Huzi Blogs
BlogCategories
BlogCategories
Disclaimer & Data Privacy Policy
Project by huzi.pk

© 2026 blogs.huzi.pk. All Rights Reserved.

    Back to all posts
    Programming

    Beyond the Wait: Master Async/Await in JavaScript (2026)

    By Huzi

    JavaScript runs one thing at a time, which makes waiting the language's central design problem. Fetching a profile, uploading a video, streaming tokens from a model — if the thread paused for any of it, the page would freeze. Async/await, nine years mature, is how modern JavaScript waits well.

    From pyramids to await

    Callbacks nested into pyramids, Promises flattened them into .then chains, and ES2017's async/await made asynchronous code read like synchronous code with try/catch intact. Top-level await now works in ES modules, so configuration and data loading happen before anything exports, no wrapper function needed. And Node ships a native global fetch — the old bootstrap ceremony is gone for good.

    The shape of a modern fetch

    One function, one try block, one honest error:

    async function fetchUser() {
      try {
        const res = await fetch("https://api.example.com/user");
        if (!res.ok) throw new Error(`HTTP ${res.status}`);
        return await res.json();
      } catch (err) {
        logger.error("fetch failed", { err });
        throw err;
      }
    }
    

    await pauses only the function, never the page — the event loop keeps serving clicks while the request is in flight. Catch by type when it matters: err instanceof ValidationError routes a form error back to the form and everything else to the log.

    Parallel, not serial

    The classic mistake is awaiting two independent requests one after the other and paying both latencies:

    // slow: two round trips, paid in series
    const user = await getUser();
    const posts = await getPosts();
    
    // fast: both in flight together
    const results = await Promise.all([getUser(), getPosts()]);
    

    The forEach trap still bites — its callback ignores await, so a loop of saves fires without finishing. Use for...of when order matters, Promise.all with map when it does not, and allSettled when partial failure is acceptable. For throttling huge batches, p-limit still leads. And when you need to resolve a promise from outside the executor, Promise.withResolvers is the clean modern shape instead of the old let-secret-variable dance.

    Cancel what you start

    An uncancelable request is a leak with extra steps. AbortController is the standard primitive, and fetch takes its signal directly:

    const controller = new AbortController();
    setTimeout(() => controller.abort(), 5000);
    try {
      const res = await fetch("/api/slow", { signal: controller.signal });
    } catch (err) {
      if (err.name === "AbortError") console.log("timed out");
    }
    

    In React, abort in the effect cleanup; on the server, abort when the user navigates away. For streaming, for await over response.body is exactly how the model SDKs deliver tokens as they arrive — chunks processed the moment they land.

    Async code teaches waiting as a craft; Gaza teaches what waiting can cost. Families there have carried a deferred return for generations — a promise no people should be asked to hold pending this long. May theirs resolve, with justice.

    Half of travel misery is serial waiting — token numbers, embassy queues, forms that spawn forms. We parallelize it: at HTG Travels, passport renewals, visa files and attestation runs proceed while you get on with your life, and one message gets you a status. Bring us the stack; we return a date.

    Advertisements


    You Might Also Like

    Mustard Dhanak Dress – Emb Front, Sleeves, Daman & Bottoms, Winter 3-Pc

    Mustard Dhanak Dress – Emb Front, Sleeves, Daman & Bottoms, Winter 3-Pc

    PKR 5350

    4-Stage Knife & Scissor Sharpener – Diamond, Ceramic, Tungsten, Non-Slip Red

    4-Stage Knife & Scissor Sharpener – Diamond, Ceramic, Tungsten, Non-Slip Red

    PKR 1030

    Moss-Green Dip-Dye Chiffon Unstitched 3-Pc Mehndi Dress – Zari & Pearl Work PAKISTAN

    Moss-Green Dip-Dye Chiffon Unstitched 3-Pc Mehndi Dress – Zari & Pearl Work PAKISTAN

    PKR 6500

    Maroon Raw Silk Party Dress – Emb Sleeves & Bottoms, Organza Dupatta

    Maroon Raw Silk Party Dress – Emb Sleeves & Bottoms, Organza Dupatta

    PKR 5500

    Black Twist-Lock Crossbody Bag for Girls – Vegan Leather, Adjustable Strap

    Black Twist-Lock Crossbody Bag for Girls – Vegan Leather, Adjustable Strap

    PKR 2800

    Advertisements


    Related Posts

    Programming
    Python Async/Await Deep Dive 2026: The Complete Problem-Solving Guide
    asyncio.run, gather, TaskGroup, httpx sessions and the blocking-call escape hatch — the 2026 Python async patterns that actually ship, with copy-paste code.

    By Huzi

    Read More
    Programming
    10 JavaScript Tricks That Will Save You Hours of Coding in 2026
    Ten patterns that pay rent: optional chaining, nullish coalescing, Object.groupBy, Set algebra, structuredClone, Array.fromAsync and the rest of the modern toolkit.

    By Huzi

    Read More
    Programming
    The Modern Craftsman: Essential Guide to C++ (2026)
    What modern actually means now: std::print and structured bindings, smart pointers for ownership, ranges pipelines, concepts, and std::expected error handling in C++23.

    By Huzi

    Read More
    Programming
    The Beginner's Guide to React: Building the Modern Web (2026)
    The component mental model, one-way props, the server/client split that defines modern React, and the scaffolding commands to start a real project today.

    By Huzi

    Read More
    Programming
    Rust for JavaScript Developers 2026: The Complete Problem-Solving Guide
    Ownership, borrowing, Option, Result and the ? operator, Tokio and a first Axum API — Rust explained through the JavaScript habits you already have.

    By Huzi

    Read More
    Programming
    The Connective Tissue: What is an API? (2026)
    APIs explained without the jargon: the restaurant analogy, REST versus GraphQL versus gRPC, webhooks, keys and OAuth, and the design habits that make an interface last.

    By Huzi

    Read More