🟢 EasyEvent Loop & Promises💻 Output Question

What does this output using JavaScript Temporal API and Event Loop

What does this output?

const now = Temporal.Now.plainDateTimeISO()
const timeUntil5pm = 17 - now.hour
Temporal.PlainDateTime.add(now, { hours: timeUntil5pm })
console.log(`Time until 5pm: ${timeUntil5pm} hours`)
Promise.resolve().then(() => console.log('Promise resolved'))
console.log('Main thread finished')

Correct Output

Main thread finished
Promise resolved
Time until 5pm: output depends on current time

Why this output?

Explanation: This code first calculates the time until 5pm and logs it. Then it creates a promise that resolves immediately and logs 'Promise resolved' when resolved. Because promises are asynchronous and use the event loop, 'Main thread finished' is logged before 'Promise resolved'. The order of 'Time until 5pm' and other logs can vary because its calculation depends on the current time which can affect the order of operations in the event loop.

Key Insight: JavaScript Event Loop, asynchronous operations, and Temporal API usage.

More Event Loop & Promises Output Questions

🟡 MediumClassic setTimeout vs Promise→🔴 HardNested setTimeout and Promise→🟡 Mediumasync/await execution order→🔴 HardPromise chaining order→

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz