Exploring Promise-Based Output Queries

Atul BansalAtul Bansal
4 min read

Promises Output Questions

1. What is the output of this code?

console.log("A");

setTimeout(() => {
  console.log("B");
}, 1000);

console.log("C");

setTimeout(() => {
  console.log("D");
}, 0);

console.log("E");
Click to see the output
A
C
E
D
B

2. What is the output of this code?

console.log("A");
Promise.resolve().then(() => console.log("B"));
console.log("C");
Click to see the output
A
C
B

3. What is the output of this code?

async function foo() {
  console.log("A");
  await Promise.resolve();
  console.log("B");
}
foo();
console.log("C");
Click to see the output A C B

4. What is the output of this code?

setTimeout(() => console.log("A"), 0);
Promise.resolve().then(() => console.log("B"));
console.log("C");
Click to see the output C B A

5. What is the output of this code?

const promise = new Promise((resolve, reject) => {
  console.log("A");
  resolve("B");
});
promise.then((res) => console.log(res));
console.log("C");
Click to see the output A C B

6. What is the output of this code?

const promise = new Promise((resolve, reject) => {
  console.log("A");
  resolve("B");
});
promise.then((res) => console.log(res));
setTimeout(() => console.log("C"), 0);
console.log("D");
Click to see the output A D B C

7. What is the output of this code?

console.log("A");
setTimeout(() => console.log("B"), 1000);
setTimeout(() => console.log("C"), 0);
console.log("D");
Click to see the output A D C B

8. What is the output of this code?

async function test() {
  console.log("A");
  await new Promise((resolve) => setTimeout(resolve, 1000));
  console.log("B");
}
test();
console.log("C");
Click to see the output A C B

9. What is the output of this code?

function foo() {
  return new Promise((resolve) => {
    setTimeout(() => resolve("A"), 1000);
  });
}
foo().then((res) => console.log(res));
console.log("B");
Click to see the output
B
A

10. What is the output of this code?

console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
console.log("D");
Click to see the output
A
D
C
B

11. What is the output of this code?

async function foo() {
  console.log("A");
  const res = await Promise.resolve("B");
  console.log(res);
}
foo();
console.log("C");
Click to see the output A C B

12. What is the output of this code?

setTimeout(() => console.log("A"), 0);
Promise.reject("B").catch((err) => console.log(err));
console.log("C");
Click to see the output
C
B
A

13. What is the output of this code?

async function foo() {
  try {
    console.log("A");
    throw new Error("B");
  } catch (err) {
    console.log(err.message);
  }
  console.log("C");
}
foo();
Click to see the output
A
B
C

14. What is the output of this code?

console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => {
  console.log("C");
  setTimeout(() => console.log("D"), 0);
});
console.log("E");
Click to see the output A E C B D

15. What is the output of this code?

console.log("A");
setTimeout(() => console.log("B"), 0);
setTimeout(() => console.log("C"), 1000);
console.log("D");
Click to see the output A D B C

16. What is the output of this code?

Promise.resolve()
  .then(() => {
    console.log("A");
    return Promise.resolve("B");
  })
  .then((res) => console.log(res));
console.log("C");
Click to see the output C A B

17. What is the output of this code?

async function foo() {
  console.log("A");
  return "B";
}
foo().then((res) => console.log(res));
console.log("C");
Click to see the output
A
C
B

18. What is the output of this code?

console.log("A");
Promise.resolve().then(() => {
  console.log("B");
  setTimeout(() => console.log("C"), 0);
});
console.log("D");
Click to see the output A D B C

19. What is the output of this code?

async function foo() {
  console.log("A");
  return new Promise((resolve) => setTimeout(() => resolve("B"), 1000));
}
foo().then((res) => console.log(res));
console.log("C");
Click to see the output A C B

20. What is the output of this code?

console.log("A");
Promise.reject("B").catch((err) => console.log(err));
Promise.resolve().then(() => console.log("C"));
console.log("D");
Click to see the output
A
D
B
C

0
Subscribe to my newsletter

Read articles from Atul Bansal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Atul Bansal
Atul Bansal

Full Stack web Developer with 4+ years of Experience