What if your app could instantly stop work the moment the user changes their mind?
Why AbortController for cancellation in React Native? - Purpose & Use Cases
Imagine you start loading data in your app, but the user quickly changes their mind and navigates away. Without a way to stop the loading, your app keeps fetching data unnecessarily.
Manually tracking and stopping ongoing tasks is tricky. It can cause wasted battery, slow performance, and confusing app behavior if old data arrives after the user moved on.
AbortController lets you easily cancel ongoing tasks like data fetching. You create a controller, pass its signal to the task, and call abort() to stop it anytime.
fetch(url).then(...); // no easy way to cancel ongoing fetch
const controller = new AbortController();
fetch(url, { signal: controller.signal }).then(...);
controller.abort(); // cancels fetchYou can build smooth apps that respect user actions by stopping unnecessary work instantly.
When a user types in a search box, you can cancel the previous search request if they type again quickly, so only the latest search runs.
Manual cancellation is hard and error-prone.
AbortController provides a simple way to cancel tasks.
This improves app responsiveness and resource use.
Practice
AbortController in React Native fetch requests?Solution
Step 1: Understand what AbortController does
AbortController provides a way to stop a fetch request that is still running.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.Final Answer:
To cancel an ongoing fetch request before it completes -> Option AQuick Check:
AbortController cancels fetch = B [OK]
- Thinking it speeds up fetch automatically
- Confusing cancellation with retry
- Assuming it caches data
Solution
Step 1: Check how to instantiate AbortController
It must be created with the new keyword:new AbortController().Step 2: Verify how to pass the signal to fetch
The fetch option key issignaland its value iscontroller.signal.Final Answer:
const controller = new AbortController(); fetch(url, { signal: controller.signal }); -> Option DQuick Check:
Use new and pass signal property = C [OK]
- Omitting the new keyword
- Passing controller instead of controller.signal
- Using wrong option keys like abort or cancelToken
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?Solution
Step 1: Understand what controller.abort() does
Calling abort() cancels the fetch request, triggering an error with name 'AbortError'.Step 2: Check the catch block behavior
The catch block logs the error name, so it will log 'AbortError'.Final Answer:
Error: AbortError -> Option AQuick Check:
Abort triggers AbortError catch = A [OK]
- Expecting data to log after abort
- Confusing AbortError with TypeError
- Thinking nothing logs on abort
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?Solution
Step 1: Understand timing of abort()
If abort() is called after fetch completes, it has no effect.Step 2: Check other options
Signal is assigned correctly, and abort() is called after fetch starts, so those are not issues.Final Answer:
The fetch request already completed before abort() was called -> Option CQuick Check:
Abort after fetch finishes does nothing = A [OK]
- Ignoring fetch completion timing
- Assuming abort always cancels
- Not handling errors but expecting cancel
Solution
Step 1: Setup AbortController and timeout before fetch
Create controller, then set a timeout to call abort after 3 seconds.Step 2: Pass controller.signal to fetch and handle errors
Pass signal option to fetch and catch abort errors to avoid unhandled exceptions.Final Answer:
const controller = new AbortController(); setTimeout(() => controller.abort(), 3000); fetch(url, { signal: controller.signal }) .catch(e => console.log(e.name)); -> Option BQuick Check:
Timeout abort + signal + catch = D [OK]
- Calling abort without passing signal to fetch
- Not catching abort errors
- Calling abort after fetch without signal
Ready to prove it?
Pass the test and this pattern moves unknown
