JS-Dev-101 practice question 3 of 25

Given the code below: 01 const delay = async delay => { 02 return new Promise((resolve, reject) => { 03 console.log(1); 04 setTimeout(resolve, delay); 05 }); 06 }; 07 08 const callDelay = async () => { 09 console.log(2); 10 const yup = await delay(1000); 11 console.log(3); 12 }; 13 14 console.log(4); 15 callDelay(); 16 console.log(5); What is logged to the console?

Full set: 148 questions
Answer options
A: 4 2 1 5 3 Correct answer
B: 4 2 1 5 3
C: 1 4 2 3 5
D: 4 5 1 2 3
Correct Answer:
A. 4 2 1 5 3
Explanation

The execution order is determined by the JavaScript event loop and the nature of async/await.
1. console.log(4) executes first.
2. callDelay() is invoked. console.log(2) runs.
3. delay() is called. console.log(1) runs.
4. await pauses callDelay() and returns control to the main thread.
5. console.log(5) executes.
6. After the 1000ms timeout, the promise resolves, callDelay() resumes, and console.log(3) runs. The final sequence logged is 4, 2, 1, 5, 3.

Why Incorrect

B. 4 2 1 5 3: This is the same as the correct answer, likely a typo in the question's options. It is correct.
C. 1 4 2 3 5: This order is incorrect as it misunderstands when async functions execute and when await yields control.
D. 4 5 1 2 3: This order is incorrect. It wrongly assumes the main thread completes entirely before any part of the async function runs.

References

MDN Web Docs, "Asynchronous JavaScript", JavaScript. Explains the event loop, callbacks, and async/await behavior.

MDN Web Docs, "async function", JavaScript Reference. Details that an async function executes synchronously until the first await expression.

Node.js Documentation, "The Node.js Event Loop, Timers, and process.nextTick()". Provides an in-depth guide on the phases of the event loop.

Scroll to Top