All Topics
All Topics
Technology
Technology
Design
Design
Programming
Programming
Science
Science
News
News
Gaming
Gaming
Entertainment
Entertainment
Business
Business
Finance
Finance
Sports
Sports
Health
Health
Food
Food
Travel
Travel
Art
Art
Music
Music
Books
Books
Education
Education
Politics
Politics
Personal
Personal
No algorithm. No AI slop. No ads. Just RSS. Pro-human. Indie writers. Real journalism. Open web. Chronological. Hand toasted.

Understanding JavaScript Promise Cancellation: Why It Doesn't Exist and Workaround Alternatives

By

goodoldneon

1mo ago· 10 min readenInsight

Summary

This article explores the technical challenge of canceling JavaScript promises, explaining why there's no built-in cancellation mechanism despite past proposals. It discusses how canceling arbitrary code mid-execution can leave resources in dirty states, requiring cooperative cleanup that undermines the simplicity users want. The article presents an alternative approach: using promises that never resolve as a surprisingly clean way to interrupt async functions, offering a workaround to the cancellation problem in JavaScript's async/await paradigm.

Key quotes

· 5 pulled
You can't cancel a JavaScript promise. There's no .cancel() method, no AbortController integration, no built-in way to say 'never mind, stop.'
The TC39 committee considered adding cancellation in 2016, but the proposal was withdrawn after heated debate.
Part of the problem is that cancelling arbitrary code mid-execution can leave resources in a dirty state (open handles, half-written data), so true cancellation requires cooperative cleanup, which undermines the simplicity people want from a .cancel() method.
But you can do something weirder: return a promise that never resolves, await it, and...
A promise that never resolves is a surprisingly clean way to interrupt an async function.
Snippet from the RSS feed
A promise that never resolves is a surprisingly clean way to interrupt an async function.

You might also wanna read