0
0
Node.jsframework~20 mins

AbortController for cancellation in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AbortController Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
ALogs 'AbortError' after about 1 second
BLogs 'Fetch succeeded' after 5 seconds
CThrows a SyntaxError immediately
DLogs 'TypeError' after 1 second
Attempts:
2 left
💡 Hint
Think about what happens when the abort signal triggers during a fetch.
📝 Syntax
intermediate
1: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.
Aconst controller = AbortController(); fetch(url, { signal: controller.signal });
Bconst controller = new AbortController(); fetch(url, { signal: controller.signal });
Cconst controller = new AbortController; fetch(url, { signal: controller });
Dconst controller = new AbortController(); fetch(url, { signal: controller });
Attempts:
2 left
💡 Hint
Remember how to instantiate classes and what fetch expects for the signal option.
🔧 Debug
advanced
2: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);
AThe fetch call does not use the controller.signal, so abort has no effect
BAbortController is not supported in Node.js
Ccontroller.abort() is called too late to affect fetch
Dfetch automatically ignores abort signals
Attempts:
2 left
💡 Hint
Check if the fetch call is connected to the AbortController signal.
state_output
advanced
1: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);
Atrue then true
Btrue then false
Cfalse then false
Dfalse then true
Attempts:
2 left
💡 Hint
Check the initial and post-abort values of the aborted property.
🧠 Conceptual
expert
1:30remaining
Why use AbortController with fetch in Node.js?
Which is the main reason to use AbortController with fetch in Node.js?
ATo convert fetch responses into streams
BTo automatically retry failed fetch requests
CTo cancel an ongoing fetch request to save resources or respond to user actions
DTo increase fetch request timeout beyond default limits
Attempts:
2 left
💡 Hint
Think about what aborting a fetch achieves.