How to cancel an asynchronous call?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
You cannot cancel every asynchronous operation the same way, because cancellation only works when the underlying API supports it. In modern JavaScript, the usual answer is AbortController for APIs such as fetch, plus explicit cancellation logic for timers, streams, or custom async workflows.
Promises Themselves Are Not Cancellable
A Promise represents a future result, but the Promise object itself does not provide a built-in cancel method.
This means code like this is not a thing in standard JavaScript:
To cancel work, you need an async API that understands cancellation and exposes a control mechanism for it.
Use AbortController With fetch
The standard pattern for network requests is:
That works because fetch is designed to respect the abort signal.
This is the most common real-world answer for browser and modern Node.js HTTP request cancellation.
Async/Await Uses The Same Mechanism
With async and await, the cancellation mechanism does not change:
async and await change the syntax, not the cancellation model.
Timers And Custom Work Need Different Logic
For timers, use the matching timer API:
For custom async functions, you often pass an AbortSignal and check it yourself:
This is how you make your own async code cancellable instead of only the built-in browser APIs.
Sometimes The Right Move Is To Ignore Stale Results
In UI code, true cancellation is not always necessary. Sometimes the practical goal is just to ignore results from an outdated request:
This is common in typeahead search, where the user keeps typing and older responses should not overwrite newer ones.
That is not the same as cancelling the network operation, but it often solves the actual UI problem.
In practice, many responsive interfaces use both techniques together: cancel what can be cancelled, and still guard against stale responses in case cancellation arrives too late or the underlying API ignores it.
Common Pitfalls
One common mistake is assuming Promise objects are cancellable by default.
Another issue is calling abort() without handling the resulting AbortError, which turns expected cancellation into noisy error logging.
A third problem is forgetting that cancellation only works if the underlying API cooperates. Wrapping a non-cancellable operation in a Promise does not magically make it cancellable.
Finally, some interfaces need stale-result protection more than true cancellation, especially in search and navigation-heavy UIs.
Summary
- Standard Promises are not cancellable by themselves.
- Use
AbortControllerwhen the async API supportsAbortSignal, especially withfetch. - Use the matching cancel API for timers, streams, or custom async work.
- '
asyncandawaitdo not change how cancellation works.' - In some UI flows, ignoring stale results is more important than truly stopping the underlying request.
Related reading
- How to change basePath for Springfox Swagger 2.0
- How to change broadcasted ip in tomcat cluster
- how to change Kafka broker list ip
- How to change swagger-ui.html default path
- How to cancel asynchronous process in javascript?
- How to catch an Exception from a thread
- How to cancel JavaScript sleep?
- How to catch errors in synchronous functions in node.js?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.