The Fetch API supports cancelling requests using AbortController
interface. We can therefore cancel in-flight requests in our applications without the need for including external fetching libraries like axios.
Example of the `AbortController` API
Above we can see how we can use an AbortController
to cancel an in-flight fetch request.
But this basic example is not indicative of how you would use this API in your applications. Let’s instead look at a real world example.
Cancelling Fetch Requests in React Applications
We will create a React application that allows users to type in a search term. Then our application will call an API, and return a list of matching users. We will call the API on every change of our input (we won’t debounce).
If there is an in flight request while the search term changes, we would like to cancel that request.
So let’s get started with a fresh NextJS application
npx create-next-app --example with-typescript cancel-fetch
Our API
Next, let’s open up the api
that our example application starts with. Open the file api/users/index.ts
. We will make a few modifications to the file.
- In order to simulate real network requests, we will add two second timeout before our API call is returned. This makes it easier to test that our cancellations are working.
- We use the
query
param ofreq
to filter out users who’s names don’t include this string.
Below you can see the modifications we made to the api/users/index.ts
file.
Our Application
Now that we have our API work out of the way, let’s get started with our React application.
Our application will require two state variables. One to keep track of the search term, and another to keep track of the array of users we receive from the server. We can define our state variables as follows.
const [searchTerm, setSearchTerm] = useState("");
const [users, setUsers] = useState<User[]>([]);
Now that we have our state, we can define a useEffect
hook where we call our API.
Now everytime a user types into our search input, we call our API and update our users
state variable with the result. Notice how we haven’t yet added our cancellation feature.
https://ceds.ed.gov/ppv/bal-v-nal-h01.html
https://ceds.ed.gov/ppv/bal-v-nal-h02.html
https://ceds.ed.gov/ppv/bal-v-nal-h03.html
https://ceds.ed.gov/ppv/bal-v-nal-h04.html
https://ceds.ed.gov/ppv/bal-v-nal-h05.html
https://ceds.ed.gov/ppv/bal-v-nal-h06.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h01.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h02.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h03.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h04.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h05.html
https://www.un.org/sites/www.iamladp.org/files/webform/bal-v-nal-h06.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h01.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h02.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h03.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h04.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h05.html
https://www.omicsonline.org/open-access-pdfs/kyir/jala/bal-v-nal-h06.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G00.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G01.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G02.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G03.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G04.html
http://admin.maps.bpex.org.uk/bll/cll/SC-v-G05.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix00.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix01.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix02.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix03.html
http://admin.maps.bpex.org.uk/bll/cll/Texas-v-LSU-liv-lix04.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live00.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live01.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live02.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live03.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live04.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live05.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live06.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live07.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live08.html
http://admin.maps.bpex.org.uk/bll/dll/Box-live09.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T0.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T1.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T2.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T3.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T4.html
http://admin.maps.bpex.org.uk/bll/dll/R-v-T5.html
https://www.hotel-alley.com/bll/dll/Box-live00.html
https://www.hotel-alley.com/bll/dll/Box-live01.html
https://www.hotel-alley.com/bll/dll/Box-live02.html
https://www.hotel-alley.com/bll/dll/Box-live03.html
https://www.hotel-alley.com/bll/dll/Box-live04.html
https://www.hotel-alley.com/bll/dll/Box-live05.html
https://www.hotel-alley.com/bll/dll/Box-live06.html
https://www.hotel-alley.com/bll/dll/Box-live07.html
https://www.hotel-alley.com/bll/dll/Box-live08.html
https://www.hotel-alley.com/bll/dll/Box-live09.html
https://www.hotel-alley.com/bll/dll/R-v-T0.html
https://www.hotel-alley.com/bll/dll/R-v-T1.html
https://www.hotel-alley.com/bll/dll/R-v-T2.html
https://www.hotel-alley.com/bll/dll/R-v-T3.html
https://www.hotel-alley.com/bll/dll/R-v-T4.html
https://www.hotel-alley.com/bll/dll/R-v-T5.html
https://www.hotel-alley.com/bll/cll/SC-v-G00.html
https://www.hotel-alley.com/bll/cll/SC-v-G01.html
https://www.hotel-alley.com/bll/cll/SC-v-G02.html
https://www.hotel-alley.com/bll/cll/SC-v-G03.html
https://www.hotel-alley.com/bll/cll/SC-v-G04.html
https://www.hotel-alley.com/bll/cll/SC-v-G05.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix00.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix01.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix02.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix03.html
https://www.hotel-alley.com/bll/cll/Texas-v-LSU-liv-lix04.html
https://shawee.io/pdp/Box-live00.html
https://shawee.io/pdp/Box-live01.html
https://shawee.io/pdp/Box-live02.html
https://shawee.io/pdp/Box-live03.html
https://shawee.io/pdp/Box-live04.html
https://shawee.io/pdp/Box-live05.html
https://shawee.io/pdp/Box-live06.html
https://shawee.io/pdp/Box-live07.html
https://shawee.io/pdp/Box-live08.html
https://shawee.io/pdp/Box-live09.html
https://shawee.io/pdp/R-v-T0.html
https://shawee.io/pdp/R-v-T1.html
https://shawee.io/pdp/R-v-T2.html
https://shawee.io/pdp/R-v-T3.html
https://shawee.io/pdp/R-v-T4.html
https://shawee.io/pdp/R-v-T5.html
http://admin.maps.bpex.org.uk/bll/cll/Rupa-Car-v-Geo-j00.html
http://admin.maps.bpex.org.uk/bll/cll/Rupa-Car-v-Geo-j01.html
http://admin.maps.bpex.org.uk/bll/cll/Rupa-Car-v-Geo-j02.html
http://admin.maps.bpex.org.uk/bll/cll/Rupa-Car-v-Geo-j03.html
http://admin.maps.bpex.org.uk/bll/dll/d-v-j00.html
http://admin.maps.bpex.org.uk/bll/dll/d-v-j01.html
http://admin.maps.bpex.org.uk/bll/dll/d-v-j02.html
http://admin.maps.bpex.org.uk/bll/dll/d-v-j03.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv1.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv2.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv3.html
http://admin.maps.bpex.org.uk/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv4.html
http://admin.maps.bpex.org.uk/bll/dll/t-v-r00.html
http://admin.maps.bpex.org.uk/bll/dll/t-v-r01.html
http://admin.maps.bpex.org.uk/bll/dll/t-v-r02.html
http://admin.maps.bpex.org.uk/bll/dll/t-v-r03.html
https://www.hotel-alley.com/bll/cll/Lopa-Tex-v-LSU-j00.html
https://www.hotel-alley.com/bll/cll/Lopa-Tex-v-LSU-j01.html
https://www.hotel-alley.com/bll/cll/Lopa-Tex-v-LSU-j02.html
https://www.hotel-alley.com/bll/cll/Lopa-Tex-v-LSU-j03.html
https://www.hotel-alley.com/bll/cll/Rupa-Car-v-Geo-j00.html
https://www.hotel-alley.com/bll/cll/Rupa-Car-v-Geo-j01.html
https://www.hotel-alley.com/bll/cll/Rupa-Car-v-Geo-j02.html
https://www.hotel-alley.com/bll/cll/Rupa-Car-v-Geo-j03.html
https://www.hotel-alley.com/bll/dll/d-v-j00.html
https://www.hotel-alley.com/bll/dll/d-v-j01.html
https://www.hotel-alley.com/bll/dll/d-v-j02.html
https://www.hotel-alley.com/bll/dll/d-v-j03.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv1.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv2.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv3.html
https://www.hotel-alley.com/bll/dll/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv4.html
https://www.hotel-alley.com/bll/dll/t-v-r00.html
https://www.hotel-alley.com/bll/dll/t-v-r01.html
https://www.hotel-alley.com/bll/dll/t-v-r02.html
https://www.hotel-alley.com/bll/dll/t-v-r03.html
http://admin.maps.bpex.org.uk/bll/cll/Lopa-Tex-v-LSU-j00.html
http://admin.maps.bpex.org.uk/bll/cll/Lopa-Tex-v-LSU-j01.html
http://admin.maps.bpex.org.uk/bll/cll/Lopa-Tex-v-LSU-j02.html
http://admin.maps.bpex.org.uk/bll/cll/Lopa-Tex-v-LSU-j03.html
https://shawee.io/pdp/d-v-j00.html
https://shawee.io/pdp/d-v-j01.html
https://shawee.io/pdp/d-v-j02.html
https://shawee.io/pdp/d-v-j03.html
https://shawee.io/pdp/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv.html
https://shawee.io/pdp/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv1.html
https://shawee.io/pdp/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv2.html
https://shawee.io/pdp/Sexy-taifur-Mik-Tyso-v-Ro-Jon-Jr-box-jp-tv3.html
https://shawee.io/pdp/t-v-r00.html
https://shawee.io/pdp/t-v-r01.html
https://shawee.io/pdp/t-v-r02.html
https://shawee.io/pdp/t-v-r03.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi1.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi2.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi3.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi4.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi5.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi6.html
http://admin.maps.bpex.org.uk/bll/dll/M-v-R-fi7.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi1.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi2.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi3.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi4.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi5.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi6.html
https://www.hotel-alley.com/bll/dll/M-v-R-fi7.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-01.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-02.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-03.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-04.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-05.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-06.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-07.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-08.html
https://www.hotel-alley.com/bll/cll/Haw-v-Nev-nc-09.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-01.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-02.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-03.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-04.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-05.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-06.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-07.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-08.html
http://admin.maps.bpex.org.uk/bll/cll/Haw-v-Nev-nc-09.html
https://shawee.io/pdp/M-v-R-fi1.html
https://shawee.io/pdp/M-v-R-fi2.html
https://shawee.io/pdp/M-v-R-fi3.html
https://shawee.io/pdp/M-v-R-fi4.html
https://shawee.io/pdp/M-v-R-fi5.html
https://shawee.io/pdp/M-v-R-fi6.html
https://shawee.io/pdp/M-v-R-fi7.html
To cancel in flight requests, we will need some mutable state to hold our AbortController
instance. For this, we can use a ref
.
const controllerRef = useRef<AbortController | null>();
We can then set and use our controllerRef
variable inside our useEffect hook.
We can modify our function as follows. Notice how the first thing we do is abort
any flight requests, then we define a new AbortController, assign it to our mutable state variable, and make our new request using that controller’s signal.
We add the catch
block because fetch
throws an error when requests are cancelled.
Now when we type into our search box, we can see the cancelled requests in our network tab.
Cancelled requests
Now we have created a React application that cancels any in flight requests using useEffect
, useState
and useRef
. To see the full application code, check out the gist below.
The full application code
Thanks for taking the time to read our tutorial on cancelling Fetch requests in real world React applications.