How can I make the console.log at the bottom wait until its all complete, then show me the answer instead of waiting?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If console.log at the bottom of your file runs before asynchronous work finishes, the problem is not console.log. The problem is execution order. In JavaScript, timers, network calls, and promise-based work continue later, so any log outside the async flow will run immediately unless you explicitly wait by chaining or awaiting the promise.
Why The Bottom console.log Runs Too Early
JavaScript does not pause the whole program when it starts an asynchronous task. It schedules that task and continues running the rest of the current code.
This example shows the issue clearly:
The output is a promise object, not "done", because the promise has not resolved yet when console.log runs.
If you want the final value, you must wait for the promise to settle.
Use await Inside An Async Function
The cleanest modern solution is async and await.
Now console.log runs only after the asynchronous work finishes. The key idea is that await pauses the main function, not the entire JavaScript runtime.
Waiting For Multiple Async Operations
If several asynchronous operations must finish before the final log, use Promise.all.
Promise.all resolves when every promise in the array resolves. That makes it the right tool when the final answer depends on all results.
If You Cannot Use await
You can still chain .then(...) directly:
This is functionally correct. It is just less readable once the flow becomes more complex.
For multiple promises:
The rule is the same: put the final log inside the async continuation.
Returning Values Instead Of Mutating Outer Variables
Another common problem is trying to assign async results to outer variables and log them later:
This logs undefined, because the assignment happens later.
Instead, return the value through the promise chain:
This is easier to reason about because data moves through the async flow rather than through timing-dependent shared state.
Error Handling Matters Too
If you are waiting for async work, also handle failures. Otherwise the final log may never run or may fail unexpectedly.
With Promise.all, remember that one rejected promise rejects the whole group. If you need per-task success and failure reporting, use Promise.allSettled instead.
The Practical Rule
If the final output depends on asynchronous work, the final console.log must live in one of these places:
- after an
await - inside a
.then(...)callback - after
await Promise.all(...)for grouped tasks
Trying to place it "at the bottom" of the file while the async tasks are still unresolved is not a formatting issue. It is a control-flow issue.
Common Pitfalls
- Logging a promise object instead of its resolved value.
- Assigning async results to an outer variable and reading that variable too early.
- Forgetting that
awaitonly works inside anasyncfunction. - Using
forEachwith async callbacks and expecting the outer code to wait. - Using
Promise.allwhen one failure should not cancel every result. In that case, usePromise.allSettled.
Summary
- '
console.logdoes not wait for asynchronous work by itself.' - Use
awaitor.then(...)to run logging after the promise resolves. - Use
Promise.allwhen you need to wait for several async operations. - Keep the final log inside the async flow, not below it in synchronous code.
- Return async values through promises instead of relying on outer mutable variables.
Related reading
- How can I notify an async routine from a sync routine?
- How can I pass a parameter to a Java Thread?
- How can I pool channels in rabbitmq?
- How can I prevent synchronous continuations on a Task?
- How can I map a DynamoDB AttributeMap type to an interface?
- How can I pass a parameter to a setTimeout() callback?
- How can I measure thread stack depth?
- How can I overcome "datetime.datetime not JSON serializable"?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseInterview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.