Repository navigation
feat(promise): add AbortSignal support to timeout and withTimeout - #1617
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Thanks for the PR! The approach here wires So I pushed a commit that changes it: on abort, Thanks again for contributing, and please keep it coming next time too! |
e23577e to
c64cc88
Compare
timeout() exists only to lose a Promise.race, so aborting it should stop it from firing (stay pending) rather than reject with AbortError. Delegating to delay() inherited delay's reject-on-abort, which is right for delay (awaited directly) but inverted for timeout: it made the timeout fire earlier and rejected withTimeout while run() kept running. Now timeout clears its own timer and stays pending on abort, and withTimeout forwards the signal so aborting lifts the time limit. The timer and abort listener are cleaned up, so the pending promise is garbage collected. Also documents the signal option for timeout and withTimeout in all four languages.
c64cc88 to
53e9358
Compare
# Conflicts: # src/promise/timeout.ts # src/promise/withTimeout.ts
Description
This PR adds AbortSignal support to the
timeoutandwithTimeoututilities ines-toolkitso that timeouts can be cancelled viaAbortController.Changes
timeout(ms)to accept an optionalAbortSignalviaTimeoutOptionsand pass it through todelaywithTimeout(run, ms)to accept an optionalAbortSignalviaWithTimeoutOptionsand pass it through totimeoutAbortControllerand aborted operationsAbortErrorwhen the operation is aborted viaAbortSignalMotivation
Some consumers need to cancel pending timeouts when the surrounding operation is cancelled (for example, when an HTTP request is aborted). By wiring
AbortSignalsupport throughtimeoutandwithTimeout, we enable safer cancellation semantics and better resource cleanup without changing existing call sites.Breaking Changes
None. Existing calls without
optionscontinue to work as before, and Abort-related behavior is opt‑in via thesignaloption.