Bird
0
0

Identify the error in this code snippet that uses AbortController:

medium📝 Debug Q14 of 15
Node.js - Timers and Scheduling
Identify the error in this code snippet that uses AbortController:
const controller = new AbortController();
const signal = controller.signal;

fetch('https://example.com', { signal });
controller.abort();

signal.addEventListener('abort', () => console.log('Fetch aborted'));
ANot passing the signal to fetch options
BCalling abort() before adding the abort event listener
CUsing signal.addEventListener instead of controller.addEventListener
DMissing async/await for fetch
Step-by-Step Solution
Solution:
  1. Step 1: Check event listener timing

    The abort event listener is added after calling controller.abort(), so it misses the event.
  2. Step 2: Verify other parts

    The signal is correctly passed to fetch, and signal is the right object to listen on. Async/await is optional here.
  3. Final Answer:

    Calling abort() before adding the abort event listener -> Option B
  4. Quick Check:

    Add event listener before abort() call = A [OK]
Quick Trick: Add abort listener before calling abort() [OK]
Common Mistakes:
  • Adding listener after abort() call
  • Listening on controller instead of signal
  • Forgetting to pass signal to fetch

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Node.js Quizzes