0
0
React Nativemobile~20 mins

AbortController for cancellation in React Native - 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!
ui_behavior
intermediate
2: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();
ALogs 'AbortError' to the console
BLogs 'TypeError' to the console
CLogs 'Data received' to the console
DFetch completes normally without any error
Attempts:
2 left
💡 Hint
Think about what happens when a fetch is cancelled using AbortController.
🧠 Conceptual
intermediate
1: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?
AIt encrypts the fetch request data for security
BIt caches fetch responses for offline use
CIt automatically retries failed fetch requests
DIt allows cancelling ongoing fetch requests to save resources and avoid unwanted updates
Attempts:
2 left
💡 Hint
Think about what happens if a user navigates away before a fetch finishes.
📝 Syntax
advanced
2:00remaining
Identify the correct way to use AbortController with fetch
Which code snippet correctly uses AbortController to cancel a fetch request in React Native?
A
const controller = new AbortController();
fetch(url, { signal: controller.signal });
controller.abort();
B
const controller = new AbortController();
fetch(url, { abortSignal: controller.signal });
controller.abort();
C
const controller = new AbortController();
fetch(url, { signal: controller });
controller.abort();
D
const controller = new AbortController();
fetch(url, { signal: controller.abort });
controller.abort();
Attempts:
2 left
💡 Hint
Check the property name used to pass the signal to fetch.
lifecycle
advanced
2: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?
AAfter the fetch request completes successfully
BInside the cleanup function returned by useEffect
CImmediately after starting the fetch request
DOnly when the user presses a cancel button
Attempts:
2 left
💡 Hint
Think about component unmounting and cleaning up side effects.
🔧 Debug
expert
3: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();
AThe abort() method must be called before fetchData()
BAbortController is not supported in React Native
CThe fetch call does not use the controller.signal, so abort() has no effect
DThe fetch URL is incorrect, so abort() is ignored
Attempts:
2 left
💡 Hint
Check how the signal is passed to fetch.