Urgent.News

What's breaking now, across hundreds of outlets.

Tech

I Replaced a Polling setInterval With an AbortController Timeout

A 2-second polling loop fired roughly 1,800 fetch requests an hour just to catch one status change. An AbortController-guarded fetch with a 30-second timeout cut that to under 10 requests an hour. The new approach still caught a stalled job within 30 seconds, the same ceiling the polling loop had. Polling still wins only when the client can't hold a connection open, which ruled it out for my own…

The original approach of polling an endpoint every two seconds to check the status of a video export resulted in roughly 1,800 fetch requests per hour, just to catch one status change. This was unnecessarily taxing on the server and on the user's device, as the radio would wake up every two seconds, even when the screen was off.

By replacing the polling loop with a single fetch call wrapped in an AbortController timeout, the requests were reduced to under 10 per hour, without compromising the speed the UI needed. The AbortController timeout allowed the client to abort the fetch request if it didn't complete within a certain time frame, in this case, 30 seconds.

The main advantage of this approach was that it reduced the number of requests made to the server, thus decreasing server load and avoiding a spike in 429 errors caused by an overload of requests. The client still needed to call this function in a loop, but now the loop only re-fires when a request ends, either with an answer or with the abort.

In the case of a stalled job, the AbortController timeout ensured that the request was held open for only 30 seconds, after which the job status was returned as "still running." This approach maintained the same detection time as the polling version, usually within a second, but significantly reduced the request count.

However, a potential downside of this method was that a dropped connection could affect the reliability of the abort version, as it was more susceptible to mid-request failures compared to the polling loop. To mitigate this, a simple retry mechanism was added around the abort version, which only took four lines of code to implement.

In conclusion, replacing the polling loop with an AbortController timeout proved to be an efficient and effective solution for reducing the number of requests made to the server, while maintaining the same detection time for job completion. The only drawback was the occasional sensitivity to dropped connections, which could be addressed with a simple retry mechanism.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

More from Sunday, 29 November 2026 →