TryBugHunt

[object Promise] in output

[object Promise] in output in JavaScript means a promise was used where its resolved value was expected — nearly always a missing await.

JavaScriptPattern: Async and race conditions

How do I fix [object Promise] in output?

To fix [object Promise] in output in JavaScript, locate the line causing the error and check the values right before it runs. Most occurrences happen because of missing await. Apply a defensive check or safe fallback, make sure collections or objects exist before access, and verify return values from upstream functions.

1. Inspect bounds & typesPrint or inspect collection sizes and verify variables are not empty or None/undefined.
2. Guard with safe accessUse optional chaining (?.), dictionary get(), or guard conditions before dereferencing.
3. Trace upstream originsFind where the value was instantiated or returned rather than only patching the symptom.

What does [object Promise] in output mean?

An async function always returns a promise. Using that promise in a string or a calculation converts it via toString, giving [object Promise]. The count or shape often looks right, which is why it slips through.

Why does [object Promise] in output happen?

1. Missing await

The promise itself flows onward instead of the value.

Breaks

const total = getTotal();
console.log(`Total: ${total}`);

Works

const total = await getTotal();
console.log(`Total: ${total}`);

2. map with an async callback

map gives an array of promises. The length is right, which is the trap.

Breaks

const names = ids.map(async id => fetchName(id));

Works

const names = await Promise.all(ids.map(id => fetchName(id)));

3. reduce with an async callback

The accumulator is a promise from the second iteration onward.

Breaks

nums.reduce(async (acc, n) => acc + await f(n), 0);

Works

nums.reduce(async (acc, n) => (await acc) + await f(n), Promise.resolve(0));

How to find it in your own code

If you see [object Promise], or typeof gives 'object' where you expected a number or string, look for the await you dropped. Promise.all is what you want whenever you need every result rather than the first.

Try it: fix this bug

medium

await sumDelayed([1, 2, 3]) gives "0[object Promise][object Promise][object Promise]".

function delay(n) {
    return new Promise((resolve) => setTimeout(() => resolve(n), 1));
}

async function sumDelayed(nums) {
    let total = 0;
    for (const n of nums) {
        total += delay(n);
    }
    return total;
}
Open with hintsRuns in your browser. No account needed.

More challenges with this error

Each of these challenges runs into this error or pattern. Find the bug and fix it in your browser — no install, no account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.