LearnFrenzy
Sign in Take a free test

Event Loop · Exercise 3 of 4

Event Loop Find the Error

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen event-loop programs: SyntaxErrors from await and yield in timer callbacks; runtime errors from setTimeout misuse, errors thrown in later callbacks and values read too early; and silent bugs from blocking loops, microtask starvation, timers relied on for ordering, inconsistent sync/async callbacks and fake sleep functions.

Question 11 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function showSpinner() { console.log("spinner shown"); }
function render(data) { console.log("render", data); }

setTimeout(showSpinner, 0);                       // show the spinner "first"
Promise.resolve(["Asha", "Ravi"]).then(render);  // cached data, already available
A setTimeout queues the spinner as a task, which runs after the promise microtask – call showSpinner() synchronously before starting the load
B render must be async
C Promise.resolve cannot hold arrays
D setTimeout delay must be negative
Question page & discussion
Question 12 Hard

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let cancelled = false;
setTimeout(() => { cancelled = true; }, 0);       // the user clicks Cancel straight away

function processItem(i) {
  if (i < 5 && !cancelled) {
    console.log("processed item", i);
    queueMicrotask(() => processItem(i + 1));      // "let other work run"
  }
}
processItem(0);
A cancelled must be const
B The condition should use ||
C queueMicrotask runs callbacks in reverse
D queueMicrotask never yields to tasks, so the cancel timer cannot run until all items are done – schedule the next step with setTimeout to let tasks in
Question page & discussion
Question 13 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let ready = false;
setTimeout(() => { ready = true; }, 0);           // "initialise first"
Promise.resolve().then(() => {
  console.log(ready ? "starting app" : "not ready");
});
A The promise callback is a microtask and runs before the zero-delay timer – do the initialisation synchronously or chain the start on the initialisation promise
B ready must be declared with var
C The ternary operator is reversed
D then callbacks cannot read outer variables
Question page & discussion
Question 14 Hard

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const cache = new Map();
function getUser(id, cb) {
  if (cache.has(id)) return cb(cache.get(id));           // cached: called synchronously
  setTimeout(() => { cache.set(id, "Asha"); cb("Asha"); }, 0);
}
function load(label, next) {
  console.log(label, "before");
  getUser(1, (u) => console.log(label, "got", u));
  console.log(label, "after");
  if (next) setTimeout(next, 5);
}
load("first:", () => load("second:"));
A The cache key must be a string
B setTimeout must use a 5 ms delay
C getUser calls its callback synchronously when cached but asynchronously otherwise – always defer it (e.g. queueMicrotask(() => cb(…)))
D Map.get returns a promise
Question page & discussion
Question 15 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function wait(ms) {
  setTimeout(() => {}, ms);            // returns undefined, not a promise
}
async function run() {
  console.log("start");
  await wait(50);
  console.log("50 ms later?");
}
setTimeout(() => console.log("10 ms timer"), 10);
run();
A await needs a number
B setTimeout callbacks must not be empty
C run must be called with await
D wait returns undefined, so await only waits one microtask – return new Promise((r) => setTimeout(r, ms))
Question page & discussion
Up next · Exercise 4Event Loop Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links