Why Your Async Function Returns Promise { <pending> } (and How to Fix It)
Why console.log shows Promise pending, how async functions really return values, and the async/await mistakes behind most JavaScript timing bugs — forEach with async, missing await, sequential awaits and unhandled rejections.
By Software Side Hustles Editorial Team · Published · Updated · 4 min read
Quick answer: an async function always returns a Promise, immediately, before its work has finished. Logging the result shows Promise { <pending> }. To get the value, await it inside another async function (or at the top level of an ES module), or use .then(value => ...). You cannot turn asynchronous code back into synchronous code — the caller has to wait too.
Why you see Promise { <pending> }
async function getUser(id) { const res = await fetch(`/api/users/${id}`); return res.json();}
const user = getUser(1);console.log(user); // Promise { <pending> }console.log(user.name); // undefinedgetUser starts the request and returns a Promise right away. The value arrives later. Fix:
const user = await getUser(1); // inside an async function or ES moduleconsole.log(user.name);
// orgetUser(1).then((user) => console.log(user.name));The return inside an async function does not return to the caller directly — it resolves the Promise the caller already holds.
"But I can't use await here"
await only works inside async functions and at the top level of ES modules ("type": "module" in package.json, .mjs files, or <script type="module">). In a CommonJS script, wrap the code:
(async () => { const user = await getUser(1); console.log(user.name);})();Async-ness is "contagious": once a function awaits, its callers must await it as well. That is expected, not a design flaw.
Mistake 1: async inside forEach
const ids = [1, 2, 3];ids.forEach(async (id) => { await saveUser(id);});console.log("All saved"); // runs BEFORE any save finishesforEach ignores the Promises returned by its callback; it does not wait. Use for...of for one-at-a-time processing, or Promise.all to run them concurrently and wait for all:
for (const id of ids) { await saveUser(id); // sequential}
await Promise.all(ids.map((id) => saveUser(id))); // concurrentconsole.log("All saved"); // now trueThe same problem affects map without Promise.all, filter with an async predicate (every Promise is truthy, so nothing is filtered out) and reduce with an async reducer.
Mistake 2: forgetting to await
async function handler(req, res) { const order = db.findOrder(req.params.id); // missing await if (!order) return res.status(404).end(); // a Promise is truthy — never 404s res.json(order); // sends {}}No error is thrown; the logic is silently wrong. TypeScript's @typescript-eslint/no-floating-promises and no-misused-promises rules catch most of these, and are worth enabling in any async-heavy codebase.
Mistake 3: awaiting independent work one after another
const user = await getUser(id); // 300 msconst orders = await getOrders(id); // 300 ms — waits for user firstIf the calls do not depend on each other, start them together:
const [user, orders] = await Promise.all([getUser(id), getOrders(id)]); // ~300 msPromise.all, allSettled, race and any
| Method | Resolves when | Rejects when |
|---|---|---|
Promise.all | all fulfil | any rejects (fail fast) |
Promise.allSettled | all finish, with each outcome | never |
Promise.race | the first settles | the first settles with a rejection |
Promise.any | the first fulfils | all reject |
Use allSettled when partial success is acceptable — sending a batch of notifications, for example.
Mistake 4: unlimited concurrency
await Promise.all(tenThousandIds.map(fetchUser));This fires ten thousand requests at once, which can exhaust sockets, hit rate limits or overload the API. Process in batches or use a small concurrency limiter:
async function mapWithLimit(items, limit, fn) { const results = new Array(items.length); let next = 0; async function worker() { while (next < items.length) { const i = next++; results[i] = await fn(items[i]); } } await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker)); return results;}
const users = await mapWithLimit(ids, 5, fetchUser);next++ is safe here because JavaScript runs this code on a single thread; there is no race between workers reading and incrementing it. Libraries such as p-limit do the same job.
Mistake 5: unhandled rejections
async function load() { const data = await fetchData(); // throws on network failure}load(); // no await, no catchSince Node.js 15, an unhandled rejection crashes the process by default. In browsers it logs an error and the failure goes unnoticed. Always await inside a try/catch, or attach .catch() at the top level:
try { await load();} catch (err) { console.error("Load failed:", err);}Also remember fetch only rejects on network errors — a 404 or 500 resolves normally. Check res.ok, as shown in parsing JSON safely.
How the event loop explains all of this
JavaScript runs your code on one thread. await pauses that function, lets other work run, and resumes it when the Promise settles. Code after a non-awaited call runs immediately. Once you picture "start the work, come back later", the output order of console.log calls stops being surprising. For how this differs from threads, see concurrency vs parallelism.
Frequently asked questions
How do I return a value from an async function?
You return it with return as usual; the caller receives a Promise for it and must await it or call .then.
Is return await necessary?
Usually not, but inside a try/catch it matters: return await promise lets the surrounding catch handle the rejection, while return promise does not.
Can I make an async function synchronous?
No. You can only wait for it from code that is itself asynchronous.
Why does await in a loop make my code slow?
Each iteration waits for the previous one. If the iterations are independent, use Promise.all (with a concurrency limit for large inputs).
Where this skill pays off
"Fetch every user, at most five at a time" is one of the exercises in our response evaluation lab: plausible AI answers that ignore the limit or lose results. Reviewing asynchronous code for correctness is skilled, paid work — read how developers earn money training AI models.