Promises have no cancel method. A promise only reports an outcome, so you cancel the work, with a signal that the work agrees to watch.
AbortControllerowns the switch.controller.signalis anAbortSignalyou hand out.controller.abort(reason)setssignal.abortedtotrue, storessignal.reason, and fires anabortevent once.abort()with no reason uses anAbortErrorDOMException.- APIs that accept
{ signal }do the rest.fetchrejects withsignal.reason, and Node'stimers/promises,events.onceand streams accept signals too. - Your own functions must cooperate. Check
signal.throwIfAborted()at the start and between steps, add anabortlistener to stop timers or requests, reject withsignal.reason, and remove the listener when done. AbortSignal.timeout(ms)returns a signal that aborts aftermswith aTimeoutError. It is the clean replacement forPromise.racewith a timer, because it cancels the work.AbortSignal.any([a, b])returns a signal that aborts when any input aborts, with that input's reason. Use it to combine a user cancel with a timeout. Both static methods exist in Node 24; browsers support them in current versions (check MDN's compatibility table for older ones).- Aborting is cooperative. If the work ignores the signal, it keeps running. Abort only changes work that is built to listen.