• Subscribes to an AbortSignal and invokes the provided callback when the signal is aborted.

    If the signal is already aborted at the time of subscription the callback is invoked immediately. Otherwise, the callback is registered as a one-time listener for the abort event.

    An unsubscribe function is returned to remove the event listener allowing proper cleanup of resources.

    This utility provides a consistent mechanism for integrating cooperative cancellation into long-running operations such as streams, async iterators, and command pipelines.

    Parameters

    • signal: AbortSignal

      An optional AbortSignal to observe.

    • onAbort: (reason: any) => void

      A callback invoked when the signal is aborted. Receives the abort reason if any.

    Returns () => void

    A function that removes the abort event listener. Safe to call multiple times.

    const unsubscribe = subscribeAbort(signal, (reason) =>
    {
    stream.destroy(reason);
    });

    try
    {
    // perform async work
    }
    finally
    {
    unsubscribe();
    }
    // Pre-aborted signals invoke the callback immediately
    const controller = new AbortController();
    controller.abort('stop');

    subscribeAbort(controller.signal, (reason) =>
    {
    console.log(reason); // 'stop'
    });