abort

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package abort provides Go analogues of the JavaScript AbortController and AbortSignal. A Controller hands out a Signal that cancellable work can observe; calling Controller.Abort fires the signal, exactly as controller.abort() does in the browser.

The signal wraps a context.Context internally, so it bridges cleanly to the standard library: pass Signal.Context to any context-aware Go API and the underlying call is cancelled too.

Index

Constants

This section is empty.

Variables

View Source
var ErrAborted = fmt.Errorf("abort: aborted: %w", context.Canceled)

ErrAborted is the reason a Signal reports once aborted — the analogue of the AbortError a JavaScript signal rejects with. It wraps context.Canceled, so both errors.Is(err, ErrAborted) and errors.Is(err, context.Canceled) report true.

Functions

This section is empty.

Types

type Controller

type Controller struct {
	// contains filtered or unexported fields
}

Controller creates and aborts a Signal, mirroring the JavaScript AbortController: hold the controller, hand its Signal to the work, and call Abort to cancel.

func From

func From(parent *Signal) *Controller

From returns a Controller whose Signal is also aborted when parent is done, so an outer signal cascades to inner work — the analogue of chaining an AbortSignal. Passing a nil parent behaves like NewController.

func NewController

func NewController() *Controller

NewController returns a fresh Controller whose Signal is not yet aborted, mirroring new AbortController().

func (*Controller) Abort

func (c *Controller) Abort()

Abort fires the controller's Signal. It is idempotent and safe to call from multiple goroutines — the analogue of controller.abort().

func (*Controller) Signal

func (c *Controller) Signal() *Signal

Signal returns the Signal this controller aborts.

type Signal

type Signal struct {
	// contains filtered or unexported fields
}

Signal is observed by cancellable work to learn when it should stop. It is the Go analogue of a JavaScript AbortSignal — the object you pass to fetch as { signal }. Obtain one from a Controller.

func (*Signal) Aborted

func (s *Signal) Aborted() bool

Aborted reports whether the signal has been aborted, mirroring the JavaScript signal.aborted property.

func (*Signal) Context

func (s *Signal) Context() context.Context

Context exposes the underlying context.Context, so the signal can be threaded into context-aware standard-library calls — the one place a context surfaces, just as a signal surfaces only at the fetch call in JavaScript.

func (*Signal) Done

func (s *Signal) Done() <-chan struct{}

Done returns a channel closed when the signal is aborted, for use in a select. It is the Go-idiomatic form of listening for the 'abort' event.

func (*Signal) Reason

func (s *Signal) Reason() error

Reason returns ErrAborted once the signal is aborted, or nil before that. A task that stops on Done typically returns Reason as its error. It mirrors the JavaScript signal.reason property.

func (*Signal) ThrowIfAborted

func (s *Signal) ThrowIfAborted() error

ThrowIfAborted returns ErrAborted if the signal has been aborted and nil otherwise, mirroring JavaScript's signal.throwIfAborted(). Call it at a safe point to bail out of long CPU-bound work:

if err := signal.ThrowIfAborted(); err != nil {
	return zero, err
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL