JavaScript
console.log
asynchronous
promises
debugging

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.

Browse interview questions

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:

javascript
1function delay(ms, value) {
2  return new Promise((resolve) => {
3    setTimeout(() => resolve(value), ms);
4  });
5}
6
7const result = delay(300, "done");
8console.log(result);

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.

javascript
1function delay(ms, value) {
2  return new Promise((resolve) => {
3    setTimeout(() => resolve(value), ms);
4  });
5}
6
7async function main() {
8  const result = await delay(300, "done");
9  console.log(result);
10}
11
12main();

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.

javascript
1function delay(ms, value) {
2  return new Promise((resolve) => {
3    setTimeout(() => resolve(value), ms);
4  });
5}
6
7async function main() {
8  const values = await Promise.all([
9    delay(200, "first"),
10    delay(100, "second"),
11    delay(300, "third")
12  ]);
13
14  console.log("All done:", values);
15}
16
17main();

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:

javascript
1function delay(ms, value) {
2  return new Promise((resolve) => {
3    setTimeout(() => resolve(value), ms);
4  });
5}
6
7delay(300, "done").then((result) => {
8  console.log(result);
9});

This is functionally correct. It is just less readable once the flow becomes more complex.

For multiple promises:

javascript
1Promise.all([
2  delay(200, "A"),
3  delay(400, "B")
4]).then((results) => {
5  console.log(results);
6});

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:

javascript
1let answer;
2
3delay(200, 42).then((value) => {
4  answer = value;
5});
6
7console.log(answer);

This logs undefined, because the assignment happens later.

Instead, return the value through the promise chain:

javascript
1async function main() {
2  const answer = await delay(200, 42);
3  console.log(answer);
4}
5
6main();

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.

javascript
1function mightFail() {
2  return Promise.reject(new Error("Request failed"));
3}
4
5async function main() {
6  try {
7    const result = await mightFail();
8    console.log(result);
9  } catch (error) {
10    console.error("Unable to finish:", error.message);
11  }
12}
13
14main();

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 await only works inside an async function.
  • Using forEach with async callbacks and expecting the outer code to wait.
  • Using Promise.all when one failure should not cancel every result. In that case, use Promise.allSettled.

Summary

  • 'console.log does not wait for asynchronous work by itself.'
  • Use await or .then(...) to run logging after the promise resolves.
  • Use Promise.all when 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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions