• Handles cooperative cancellation for an AbortSignal by invoking the provided abort callback when the signal has been aborted.

    This utility centralizes the common pattern of checking signal.aborted, executing cleanup logic, and short-circuiting control flow. It is intended for use inside loops and async operations where early termination should occur without throwing.

    Parameters

    • signal: AbortSignal

      An optional AbortSignal to check.

    • onAbort: (reason?: unknown) => void

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

    Returns boolean

    true if the signal was aborted and the callback was invoked; otherwise false.

    const abort = (reason: any) => {}

    for await (const row of stream)
    {
    if (handleAborted(signal, abort)) { return; }

    // process row
    }