Challenge - 5 Problems
AbortController Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when an AbortController aborts a fetch?
Consider this Node.js code using AbortController to cancel a fetch request. What is the output or behavior when the controller aborts the fetch?
Node.js
import fetch from 'node-fetch'; const controller = new AbortController(); const signal = controller.signal; fetch('https://httpbin.org/delay/5', { signal }) .then(() => console.log('Fetch succeeded')) .catch(err => console.log(err.name)); setTimeout(() => controller.abort(), 1000);
Attempts:
2 left
💡 Hint
Think about what happens when the abort signal triggers during a fetch.
✗ Incorrect
When the AbortController aborts, the fetch promise rejects with an error named 'AbortError'. This happens shortly after abort() is called, not after the full delay.
📝 Syntax
intermediate1:30remaining
Which option correctly creates an AbortController and uses its signal in fetch?
Select the code snippet that correctly creates an AbortController and passes its signal to fetch in Node.js.
Attempts:
2 left
💡 Hint
Remember how to instantiate classes and what fetch expects for the signal option.
✗ Incorrect
Option B correctly creates an AbortController instance and passes its signal property to fetch. Option B misses 'new', C passes the controller itself instead of its signal, and D also passes the controller instead of the signal.
🔧 Debug
advanced2:00remaining
Why does this fetch never abort despite calling controller.abort()?
Look at this code snippet. The fetch request never aborts even though controller.abort() is called. What is the reason?
Node.js
import fetch from 'node-fetch'; const controller = new AbortController(); fetch('https://httpbin.org/delay/5') .then(() => console.log('Done')) .catch(err => console.log(err.name)); setTimeout(() => controller.abort(), 1000);
Attempts:
2 left
💡 Hint
Check if the fetch call is connected to the AbortController signal.
✗ Incorrect
The fetch call must receive the signal option from the AbortController to be abortable. Here, fetch is called without the signal, so aborting the controller does nothing.
❓ state_output
advanced1:30remaining
What is the state of AbortController.signal.aborted after calling abort()?
Given this code, what is the value of signal.aborted after controller.abort() is called?
Node.js
const controller = new AbortController(); const signal = controller.signal; console.log(signal.aborted); controller.abort(); console.log(signal.aborted);
Attempts:
2 left
💡 Hint
Check the initial and post-abort values of the aborted property.
✗ Incorrect
Initially, signal.aborted is false. After calling controller.abort(), it becomes true indicating the signal is aborted.
🧠 Conceptual
expert1:30remaining
Why use AbortController with fetch in Node.js?
Which is the main reason to use AbortController with fetch in Node.js?
Attempts:
2 left
💡 Hint
Think about what aborting a fetch achieves.
✗ Incorrect
AbortController allows canceling a fetch request before it completes, useful for stopping slow or unwanted requests, saving bandwidth and CPU.