Challenge - 5 Problems
AbortController Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when AbortController aborts a fetch?
Consider this React Native code snippet using AbortController to cancel a fetch request. What will be the output or behavior when the abort() method is called before the fetch completes?
React Native
const controller = new AbortController(); fetch('https://example.com/data', { signal: controller.signal }) .then(response => response.json()) .then(data => console.log('Data received')) .catch(error => console.log(error.name)); controller.abort();
Attempts:
2 left
💡 Hint
Think about what happens when a fetch is cancelled using AbortController.
✗ Incorrect
When abort() is called on the AbortController, the fetch is cancelled and it throws an error with the name 'AbortError'. This error is caught in the catch block and logged.
🧠 Conceptual
intermediate1:30remaining
Why use AbortController in React Native fetch?
Which of the following best explains why AbortController is useful when making fetch requests in React Native?
Attempts:
2 left
💡 Hint
Think about what happens if a user navigates away before a fetch finishes.
✗ Incorrect
AbortController lets you cancel fetch requests that are no longer needed, preventing wasted network and CPU usage and avoiding state updates on unmounted components.
📝 Syntax
advanced2:00remaining
Identify the correct way to use AbortController with fetch
Which code snippet correctly uses AbortController to cancel a fetch request in React Native?
Attempts:
2 left
💡 Hint
Check the property name used to pass the signal to fetch.
✗ Incorrect
The fetch API expects the signal property to be set to controller.signal. Other options use incorrect property names or values.
❓ lifecycle
advanced2:30remaining
When should you call abort() in a React Native component?
In a React Native functional component that fetches data on mount, when is the best time to call controller.abort() to avoid memory leaks?
Attempts:
2 left
💡 Hint
Think about component unmounting and cleaning up side effects.
✗ Incorrect
Calling abort() inside the cleanup function of useEffect cancels the fetch if the component unmounts before the fetch completes, preventing memory leaks and state updates on unmounted components.
🔧 Debug
expert3:00remaining
Why does this fetch never abort?
Given this React Native code, why does calling controller.abort() not stop the fetch request?
React Native
const controller = new AbortController();
function fetchData() {
fetch('https://example.com/api', {})
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.log(err.name));
}
fetchData();
controller.abort();Attempts:
2 left
💡 Hint
Check how the signal is passed to fetch.
✗ Incorrect
The fetch call must receive the signal option from the AbortController to be cancellable. Without passing controller.signal, abort() does nothing.