SoftwareSideHustles
Debugging

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> }

javascript
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); // undefined

getUser starts the request and returns a Promise right away. The value arrives later. Fix:

javascript
const user = await getUser(1); // inside an async function or ES module
console.log(user.name);
// or
getUser(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:

javascript
(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

javascript
const ids = [1, 2, 3];
ids.forEach(async (id) => {
await saveUser(id);
});
console.log("All saved"); // runs BEFORE any save finishes

forEach 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:

javascript
for (const id of ids) {
await saveUser(id); // sequential
}
await Promise.all(ids.map((id) => saveUser(id))); // concurrent
console.log("All saved"); // now true

The 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

javascript
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

javascript
const user = await getUser(id); // 300 ms
const orders = await getOrders(id); // 300 ms — waits for user first

If the calls do not depend on each other, start them together:

javascript
const [user, orders] = await Promise.all([getUser(id), getOrders(id)]); // ~300 ms

Promise.all, allSettled, race and any

MethodResolves whenRejects when
Promise.allall fulfilany rejects (fail fast)
Promise.allSettledall finish, with each outcomenever
Promise.racethe first settlesthe first settles with a rejection
Promise.anythe first fulfilsall reject

Use allSettled when partial success is acceptable — sending a batch of notifications, for example.

Mistake 4: unlimited concurrency

javascript
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:

javascript
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

javascript
async function load() {
const data = await fetchData(); // throws on network failure
}
load(); // no await, no catch

Since 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:

javascript
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.

Cookies on this site

We use essential cookies to run the questionnaire, practice labs and sign-in. With your permission we'd also like to use analytics to understand which pages help people, and advertising cookies to measure our Google Ads. Rejecting never limits any feature. Cookie policy