Why doesn't 'await' wait for axios request completes?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When await appears not to wait for an Axios request to complete, the issue is almost always that the calling function itself is not being awaited, or await is used inside a non-async callback (like forEach). The await keyword only pauses execution within the immediate async function scope — it does not block the outer caller unless that caller also awaits the returned promise. Understanding this scoping behavior is the key to debugging "await doesn't wait" problems.
The Problem
The await inside fetchData works correctly, but the caller does not await the function's returned promise. Since every async function returns a promise, you must await or .then() the result.
Fix 1: Await the Calling Function
Every function in the call chain between the await and the top-level entry point must either await the result or handle the returned promise with .then().
Fix 2: The forEach Trap
Array.forEach ignores the promise returned by the async callback. Use for...of for sequential execution or Promise.all with .map() for concurrent requests.
Fix 3: Missing Return in Intermediate Functions
If a helper function creates a promise but doesn't return it, the caller has nothing to await.
Fix 4: Axios Interceptors and Error Handling
Common Pitfalls
- Not awaiting the async function itself:
awaitonly pauses the function it's inside. The caller must alsoawaitor.then()the returned promise, all the way up the call chain. - Using
forEachwith async callbacks:forEachignores returned promises. Usefor...offor sequential execution orPromise.all(arr.map(...))for concurrent execution. - Forgetting to return the promise: If an intermediate function calls
axios.get()withoutreturn, the caller awaitsundefinedinstead of the actual response. - Interceptors swallowing responses: Axios interceptors must return the response (or
Promise.reject(error)for errors). A missing return makes every request resolve toundefined. - Mixing
.then()andawaitincorrectly: Avoid patterns likeawait axios.get(url).then(r => r.data)— while technically valid, they're confusing. Pick one style and use it consistently.
Summary
awaitonly pauses theasyncfunction it appears in — callers must also await the resultforEachwithasynccallbacks does not wait — usefor...oforPromise.allwithmap- Always
returnpromises from intermediate functions so callers can await them - Axios interceptors must return the response or reject the error
- Wrap
awaitcalls intry/catchfor proper error handling

