Easy methods to Cancel Pending API Requests to Present Appropriate Information

No Comments

I just lately needed to create a widget in React that fetches information from a number of API endpoints. Because the consumer clicks round, new information is fetched and marshalled into the UI. But it surely brought on some issues.

One downside rapidly grew to become evident: if the consumer clicked round quick sufficient, as earlier community requests obtained resolved, the UI was up to date with incorrect, outdated information for a quick time period.

We will debounce our UI interactions, however that basically doesn’t resolve our downside. Outdated community fetches will resolve and replace our UI with incorrect information up till the ultimate community request finishes and updates our UI with the remaining right state. The issue turns into extra evident on slower connections. Moreover, we’re left with ineffective networks requests that waste the consumer’s information.

Right here is an instance I constructed as an instance the issue. It grabs sport offers from Steam by way of the cool Low-cost Shark API utilizing the fashionable fetch() technique. Strive quickly updating the worth restrict and you will notice how the UI flashes with incorrect information till it lastly settles.

CodePen Embed Fallback

The answer

Seems there’s a method to abort pending DOM asynchronous requests utilizing an AbortController. You need to use it to cancel not solely HTTP requests, however occasion listeners as effectively.

The AbortController interface represents a controller object that means that you can abort a number of Internet requests as and when desired.

Mozilla Developer Community

The AbortController API is straightforward: it exposes an AbortSignal that we insert into our fetch() calls, like so:

const abortController = new AbortController()
const sign = abortController.sign
fetch(url, { sign })

From right here on, we will name abortController.abort() to verify our pending fetch is aborted.

Let’s rewrite our instance to verify we’re canceling any pending fetches and marshalling solely the most recent information acquired from the API into our app:

CodePen Embed Fallback

The code is usually the identical with few key distinctions:

It creates a brand new cached variable, abortController, in a useRef within the <App /> part.For every new fetch, it initializes that fetch with a brand new AbortController and obtains its corresponding AbortSignal.It passes the obtained AbortSignal to the fetch() name.It aborts itself on the following fetch.

const App = () => {
// Identical as earlier than, native variable and state declaration
// …

// Create a brand new cached variable abortController in a useRef() hook
const abortController = React.useRef()

React.useEffect(() => {
// If there’s a pending fetch request with related AbortController, abort
if (abortController.present) {
// Assign a brand new AbortController for the most recent fetch to our useRef variable
abortController.present = new AbortController()
const { sign } = abortController.present

// Identical as earlier than
fetch(url, { sign }).then(res => {
// Remainder of our fetching logic, identical as earlier than
}, [


That’s it! We now have the most effective of each worlds: we debounce our UI interactions and we manually cancel outdated pending community fetches. This manner, we’re certain that our UI is up to date as soon as and solely with the newest information from our API.

The publish Easy methods to Cancel Pending API Requests to Present Appropriate Information appeared first on CSS-Methods. You’ll be able to help CSS-Methods by being an MVP Supporter.

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment