Skip to content

AbortController.prototype.timeout and AbortController.prototype.cancel #1039

Open
@jasnell

Description

@jasnell

As a complement to the new AbortSignal.timeout(), there are a couple more cases that may be worthwhile exploring, specifically with AbortController:

  • abortController.timeout(delay[, reason])

    • Sets an internal timer on the AbortController to trigger the AbortSignal with reason after delay milliseconds.
    • If there is already an active internal timer, when timeout() is called, it is reset.
    • If abortController.abort() is directly called while there is an active internal timer, the signal is immediately triggered and the internal timer is cleared.
    • This is useful for implementing an ongoing "idle timeout" mechanism (e.g. the timer is reset whenever there is new activity but the signal is triggered when activity is not frequent enough).
  • abortController.cancel()

    • Signals that the AbortController is no longer expected to trigger the AbortSignal at all, allowing the AbortSignal to clear it's 'abort' algorithms and indicating that it is expected to never trigger. This can be used as a signal that consuming code can safely ignore the signal after it becomes abandoned.
    • If called while there is an active internal timeout from abortController.timeout(), the internal timer is cleared.

Example:

const idleAborter = new AbortController();
// Controller is expected to trigger after 10 seconds
idleAborter .timeout(10_000);

const eventTarget = getSomeKindOfEventTarget({ signal: idleAborter .signal });

// Getting some kind of activity resets the internal timeout
eventTarget.addEventListener('activity', () => idleAborter.timeout(10_000));

// Signal that paying attention to the signal is no longer necessary and clears the timeout.
eventTarget.addEventListener('end', () => idleAborter.cancel());

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposalNew features or enhancementsneeds implementer interestMoving the issue forward requires implementers to express interesttopic: abortingAbortController and AbortSignal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions