Sknoe / Notes
Async Programming Without the Footguns: Throughput, Promise.all, and Timeouts
Lessons from refactoring a synchronous app to async/await—how to avoid waterfall requests, handle hanging APIs, and refactor from the bottom up.
I was looking back at notes from an old backend talk about refactoring a synchronous C# app to asynchronous code. The examples were in C#, but the question stuck with me: if you add async and await, does the app get faster?
The answer is more useful than a simple yes or no. Async code does not make one slow API call finish sooner. It can keep your application from waiting unnecessarily, and it lets independent I/O requests overlap. That difference is where throughput improves.
Async changes the waiting, not the call’s duration
Avoid a request waterfall
Suppose a dashboard needs a profile, recent orders, and saved items. If each request is independent, awaiting each one before starting the next creates a waterfall:
// Each request starts after the previous one finishes.async function loadAccountDashboard(userId) {const profile = await fetchUserProfile(userId); // about 4 secondsconst orders = await fetchUserOrders(userId); // about 2 secondsconst saved = await fetchSavedItems(userId); // about 3 secondsreturn { profile, orders, saved };}
With those example timings, the loader takes about nine seconds because the waits happen in sequence. If the calls do not depend on each other, start them together and await their results as a group:
// All three requests are started before we wait for the results.async function loadAccountDashboard(userId) {const [profile, orders, saved] = await Promise.all([fetchUserProfile(userId),fetchUserOrders(userId),fetchSavedItems(userId),]);return { profile, orders, saved };}
If those requests take about four, two, and three seconds, the combined wait is roughly four seconds—the slowest request—assuming the services and network can handle the calls concurrently. Keep the sequential version when a later request needs the earlier result. Concurrency is about independent work; it is not a speed boost for each individual request.
Under the hoodWhat Promise.all does when one request fails
Looking closer
If a dashboard can still be useful when one independent panel fails, Promise.allSettled() lets the caller inspect each result and choose what to render. If the remaining requests should stop after a failure, pass a shared AbortSignal to cancellable operations and abort them deliberately. Promise.all itself does not cancel work.
Give slow requests a timeout
Some APIs take too long to respond. Fetch supports an AbortSignal, and modern browsers and Node.js provide AbortSignal.timeout() to create one that aborts after a chosen number of milliseconds:
async function fetchPartnerInventory(sku) {try {const response = await fetch(`https://api.partner.example/stock/${sku}`,{ signal: AbortSignal.timeout(3000) },);if (!response.ok) {throw new Error(`Partner API returned ${response.status}`);}return await response.json();} catch (error) {if (error instanceof DOMException && error.name === 'TimeoutError') {throw new Error('Inventory check timed out. Please retry.', { cause: error });}throw error;}}
A timeout makes the client stop waiting for the fetch and gives your code a recognizable TimeoutError to handle. For a different kind of cancellation, create an AbortController, pass its signal to fetch, and call abort() when the operation is no longer needed. Neither client-side approach guarantees that a server which already received the request has stopped processing it. For storefront actions that change data, design server-side timeout and retry behavior carefully so a retry cannot accidentally repeat the operation.
Refactor one layer at a time
When a synchronous helper becomes asynchronous, it returns a Promise. Callers must then handle that Promise: await it, return it, or attach a Promise handler. Starting with a low-level API helper and following the Promise through its callers is one practical way to refactor a codebase. It is a guide, not a rule; the important part is to update each layer’s contract instead of treating a Promise like the final value.
Two small reminders from those old notes still help:
If you need a result or want the surrounding try/catch to handle a rejection, await the Promise or return it to a caller that handles it. A deliberately fire-and-forget call still needs a rejection handler if it can fail.
Keep pure in-memory calculations synchronous unless they truly need asynchronous work. Adding async changes the function to return a Promise; wrapping synchronous work in new Promise() does not make the calculation faster or non-blocking.
That is the part of the old talk I keep coming back to: the useful question is not “How do I make await faster?” It is “Which work actually depends on this result, and which work can start now?”
Did this click?
Tap the bulb if this explanation helped.