Table of Contents

Class AbortSignal

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The AbortSignal interface represents a signal object that allows you to communicate with an asynchronous operation (such as a fetch request) and abort it if required via an AbortController object.

[Value("AbortSignal")]
public class AbortSignal : EventTarget
Inheritance
AbortSignal
Derived
Inherited Members

Remarks

Constructors

AbortSignal()

public AbortSignal()

Properties

Aborted

The aborted read-only property returns a value that indicates whether the asynchronous operations the signal is communicating with are aborted (true) or not (false).

[Value("aborted")]
public bool Aborted { get; }

Property Value

bool

true (aborted) or false

Remarks

Onabort

[Value("onabort")]
public EventHandlerNonNull Onabort { get; set; }

Property Value

EventHandlerNonNull

Reason

The reason read-only property returns a JavaScript value that indicates the abort reason.

[Value("reason")]
public dynamic Reason { get; }

Property Value

dynamic

A JavaScript value that indicates the abort reason, or undefined, if not aborted.

Remarks

The property is undefined when the signal has not been aborted.
It can be set to a specific value when the signal is aborted, using Abort(dynamic) or AbortSignalabort.
If not explicitly set in those methods, it defaults to "AbortError" DOMException.

-Fetch API

See also on MDN

Methods

Abort(dynamic)

The abort event of the AbortSignal is fired when the associated request is aborted, i.e., using Abort(dynamic).

[Value("abort")]
public static AbortSignal Abort(dynamic reason = null)

Parameters

reason dynamic

Returns

AbortSignal

An AbortSignal instance with the Aborted property set to true, and Reason set to the specified or default reason value.

Remarks

Any(List<AbortSignal>)

The AbortSignal.any() static method takes an iterable of abort signals and returns an AbortSignal. The returned abort signal is aborted when any of the input iterable abort signals are aborted. The {{domxref("AbortSignal.reason", "abort reason",&quot;&quot;,"true")}} will be set to the reason of the first signal that is aborted. If any of the given abort signals are already aborted then so will be the returned AbortSignal.

[Value("any")]
public static AbortSignal Any(List<AbortSignal> signals)

Parameters

signals List<AbortSignal>

Returns

AbortSignal

A AbortSignal that is:

Remarks

ThrowIfAborted()

The throwIfAborted() method throws the signal's abort Reason if the signal has been aborted; otherwise it does nothing.

[Value("throwIfAborted")]
public GlobalObject.Undefined ThrowIfAborted()

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

An API that needs to support aborting can accept an AbortSignal object and use throwIfAborted() to test and throw when the abort event is signalled.

This method can also be used to abort operations at particular points in code, rather than passing to functions that take a signal.

-Fetch API

See also on MDN

Timeout(ulong)

The AbortSignal.timeout() static method returns an AbortSignal that will automatically abort after a specified time.

[Value("timeout")]
public static AbortSignal Timeout(ulong milliseconds)

Parameters

milliseconds ulong

Returns

AbortSignal

An AbortSignal.The signal will abort with its Reason property set to a TimeoutError DOMException on timeout, or an AbortError DOMException if the operation was user-triggered.

Remarks

The signal aborts with a TimeoutError DOMException on timeout.

The timeout is based on active rather than elapsed time, and will effectively be paused if the code is running in a suspended worker, or while the document is in a back-forward cache (&quot;bfcache&quot;).

To combine multiple signals, you can use AbortSignalany, for example, to directly abort a download using either a timeout signal or by calling Abort(dynamic).

See also on MDN