Bird
Raised Fist0
React Nativemobile~20 mins

AbortController for cancellation in React Native - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using AbortController in React Native fetch requests?
easy
A. To cancel an ongoing fetch request before it completes
B. To speed up the fetch request automatically
C. To retry a fetch request multiple times
D. To cache the fetch response for offline use

Solution

  1. Step 1: Understand what AbortController does

    AbortController provides a way to stop a fetch request that is still running.
  2. Step 2: Compare options with AbortController's purpose

    Only To cancel an ongoing fetch request before it completes describes stopping or canceling a fetch request early.
  3. Final Answer:

    To cancel an ongoing fetch request before it completes -> Option A
  4. Quick Check:

    AbortController cancels fetch = B [OK]
Hint: AbortController is for stopping fetch early [OK]
Common Mistakes:
  • Thinking it speeds up fetch automatically
  • Confusing cancellation with retry
  • Assuming it caches data
2. Which of the following is the correct way to create an AbortController and pass its signal to a fetch request?
easy
A. const controller = new AbortController(); fetch(url, { cancelToken: controller });
B. const controller = AbortController(); fetch(url, { abort: controller });
C. const controller = new AbortController(); fetch(url, { signal: controller })
D. const controller = new AbortController(); fetch(url, { signal: controller.signal });

Solution

  1. Step 1: Check how to instantiate AbortController

    It must be created with the new keyword: new AbortController().
  2. Step 2: Verify how to pass the signal to fetch

    The fetch option key is signal and its value is controller.signal.
  3. Final Answer:

    const controller = new AbortController(); fetch(url, { signal: controller.signal }); -> Option D
  4. Quick Check:

    Use new and pass signal property = C [OK]
Hint: Use new AbortController() and pass signal: controller.signal [OK]
Common Mistakes:
  • Omitting the new keyword
  • Passing controller instead of controller.signal
  • Using wrong option keys like abort or cancelToken
3. Consider this code snippet:
const controller = new AbortController();
fetch('https://example.com/data', { signal: controller.signal })
  .then(response => response.json())
  .then(data => console.log('Data:', data))
  .catch(error => console.log('Error:', error.name));

controller.abort();
What will be logged to the console?
medium
A. Error: AbortError
B. Data: [fetched data]
C. Error: TypeError
D. Nothing is logged

Solution

  1. Step 1: Understand what controller.abort() does

    Calling abort() cancels the fetch request, triggering an error with name 'AbortError'.
  2. Step 2: Check the catch block behavior

    The catch block logs the error name, so it will log 'AbortError'.
  3. Final Answer:

    Error: AbortError -> Option A
  4. Quick Check:

    Abort triggers AbortError catch = A [OK]
Hint: Aborting fetch triggers AbortError in catch [OK]
Common Mistakes:
  • Expecting data to log after abort
  • Confusing AbortError with TypeError
  • Thinking nothing logs on abort
4. You wrote this code but the fetch never cancels as expected:
const controller = new AbortController();
fetch('https://api.example.com', { signal: controller.signal });
// Some code
controller.abort();
What is the most likely reason the fetch is not canceled?
medium
A. You did not assign the signal property correctly
B. You called abort() before starting fetch
C. The fetch request already completed before abort() was called
D. You forgot to handle the abort error in catch block

Solution

  1. Step 1: Understand timing of abort()

    If abort() is called after fetch completes, it has no effect.
  2. Step 2: Check other options

    Signal is assigned correctly, and abort() is called after fetch starts, so those are not issues.
  3. Final Answer:

    The fetch request already completed before abort() was called -> Option C
  4. Quick Check:

    Abort after fetch finishes does nothing = A [OK]
Hint: Abort only works if fetch is still running [OK]
Common Mistakes:
  • Ignoring fetch completion timing
  • Assuming abort always cancels
  • Not handling errors but expecting cancel
5. You want to cancel a fetch request if it takes longer than 3 seconds. Which code snippet correctly implements this using AbortController?
hard
A. const controller = new AbortController(); setTimeout(() => controller.abort(), 3000); fetch(url);
B. const controller = new AbortController(); setTimeout(() => controller.abort(), 3000); fetch(url, { signal: controller.signal }) .catch(e => console.log(e.name));
C. const controller = new AbortController(); fetch(url); setTimeout(() => controller.abort(), 3000);
D. const controller = new AbortController(); fetch(url, { signal: controller }); setTimeout(() => controller.abort(), 3000);

Solution

  1. Step 1: Setup AbortController and timeout before fetch

    Create controller, then set a timeout to call abort after 3 seconds.
  2. Step 2: Pass controller.signal to fetch and handle errors

    Pass signal option to fetch and catch abort errors to avoid unhandled exceptions.
  3. Final Answer:

    const controller = new AbortController(); setTimeout(() => controller.abort(), 3000); fetch(url, { signal: controller.signal }) .catch(e => console.log(e.name)); -> Option B
  4. Quick Check:

    Timeout abort + signal + catch = D [OK]
Hint: Set timeout before fetch and pass signal with catch [OK]
Common Mistakes:
  • Calling abort without passing signal to fetch
  • Not catching abort errors
  • Calling abort after fetch without signal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard