Lab
JavaScript Engine Simulator
20 min
Intermediate3 Free Attempts
Instructions
Objective
Implement three functions that test your mastery of JavaScript async patterns — the exact skills frontend interviews assess.
Task 1: Predict Event Loop Output
Write a function predictOutput() that returns an array of strings in the exact order they would be logged:
function predictOutput() {
// Return the order of console.logs for this code:
//
// console.log('A');
// setTimeout(() => console.log('B'), 0);
// Promise.resolve()
// .then(() => console.log('C'))
// .then(() => console.log('D'));
// queueMicrotask(() => console.log('E'));
// console.log('F');
return ['A', '?', '?', '?', '?', '?']; // Replace with correct order
}
Task 2: Implement Promise.all
Write a promiseAll(promises) function that behaves identically to Promise.all:
- Resolves with an array of results when all promises fulfill
- Results must maintain the same order as the input promises
- Rejects immediately when any promise rejects
- Handles non-Promise values (wraps them with Promise.resolve)
- Handles empty array input (resolves with [])
function promiseAll(promises) {
// Your implementation here
}
Task 3: Retry with Exponential Backoff
Write a retryWithBackoff(fn, maxRetries, baseDelay) function:
- Calls
fn()and returns its result if successful - On failure, waits
baseDelay * 2^attemptmilliseconds before retrying - After
maxRetriesfailed attempts, throws the last error - Must work with both sync and async functions
async function retryWithBackoff(fn, maxRetries = 3, baseDelay = 1000) {
// Your implementation here
}
Requirements
- All three functions must be implemented
- Code must be valid JavaScript (ES2022+)
- No external libraries allowed
- Include brief comments explaining your reasoning for Task 1
Grading Rubric
Task 1: Correctly predicts the event loop output order (A, F, C, E, D, B) with accurate reasoning about microtask vs macrotask queues25 points
Task 2: Correctly implements Promise.all with proper order preservation, immediate rejection, non-Promise handling, and empty array edge case35 points
Task 3: Implements retry with correct exponential backoff timing (baseDelay * 2^attempt), proper error propagation after max retries, and works with both sync and async functions30 points
Code quality: Clean, readable code with appropriate comments explaining the event loop reasoning10 points
Your Solution
Use any programming language
3 free attempts remaining