@@ -11,25 +11,25 @@ which can be operated with an `AbortController`.
11
11
The ` AbortController ` & ` AbortSignal ` APIs can help manage operational "life time". Some examples:
12
12
13
13
- Long running or asynchronous tasks, for example timers or network fetches
14
- - Overlapping operations, for example cancelling in-flight network fetches to replace them with newer ones
14
+ - Overlapping operations, for example canceling in-flight network fetches to replace them with newer ones
15
15
- Set up and tear down, for example a Web Components ` connectedCallback ` and ` disconnectedCallback `
16
16
17
17
## AbortSignal AbortController Pattern
18
18
19
19
_ Cancellation Signals_ get given to APIs so they know when to abort. An ` AbortSignal ` is created by a _ controller_
20
20
(` new AbortSignal() ` will throw an error). _ Controllers_ allow you to make the decision of when a _ Cancellation Signal_
21
21
changes. Creating an ` AbortController ` will also create a new ` AbortSignal ` , accessible via ` .signal ` . Code that has
22
- access to the _ controller_ can determine when it should be aborted (by calling ` .abort() ` ), while code that has access
23
- to the _ signal_ can be notified of the abort. To make a new ` AbortController ` , call ` new AbortContoller() ` . The
24
- constructor takes no arguments.
22
+ access to the _ controller_ can decide when it should be aborted (by calling ` .abort() ` ), while code that has access to
23
+ the _ signal_ can be notified of the abort. To make a new ` AbortController ` , call ` new AbortContoller() ` . The constructor
24
+ takes no arguments.
25
25
26
26
An ` AbortSignal ` s ` .aborted ` property will be ` true ` if the signal has been aborted - you can periodically check that to
27
27
stop any work that is about to be done. ` AbortSignal ` is also an ` EventTarget ` - it emits an ` abort ` event which you can
28
28
listen to and invoke your tear down.
29
29
30
30
You can also create some basic _ controller-free signals_ that follow some common patterns. For example
31
- ` AbortSignal.timeout(1000) ` will create a _ cancellation signal_ that aborts after 1000ms . These _ controller-free
32
- signals_ cannot be manually aborted. However, you can _ combine_ controller-free and controllable signals with
31
+ ` AbortSignal.timeout(1000) ` will create a _ cancellation signal_ that aborts after 1000 milliseconds . These
32
+ _ controller-free signals_ cannot be manually aborted. You can _ combine_ controller-free and controllable signals with
33
33
` AbortSignal.any([...signals]) ` .
34
34
35
35
## Using Cancellation Signals internally manage your private APIs
@@ -85,12 +85,12 @@ class StopWatchElement extends HTMLElement {
85
85
86
86
## Using Cancellation Signals in your own public APIs
87
87
88
- If you can use a signal as part of your internal state, it might be simpler to provide it as part of the public API. If
89
- you are considering using _cancellation signals_ in a public API, it's a good idea to make them an optional part of your
90
- API as they won't always be _needed_.
88
+ If you can use a signal as part of your internal state, it might be simpler to add it as part of the public API. If you
89
+ are considering using _cancellation signals_ in a public API, it's a good idea to make them an optional part of your API
90
+ as they won't always be _needed_.
91
91
92
92
A component using _cancellation signals_ no longer needs separate start & stop methods, instead combining into one and
93
- relying on the signal to know when to stop. This can often simplify code as there is no need to track state across
93
+ relying on the signal to know when to stop. This can often simplify code as there's no need to track state across
94
94
different methods.
95
95
96
96
` ` ` js
@@ -125,11 +125,11 @@ class StopWatchElement extends HTMLElement {
125
125
## Combining multiple Cancellation Signals
126
126
127
127
It's possible to combine multiple sources of _cancellation signals_ - for example combining internal and external
128
- _cancellation signals_ to allow for multiple flavors of API. Two or more _cancellation signals_ can be combined into one
128
+ _cancellation signals_ to allow for multiple flavors of API. Two or more _cancellation signals_ can be joined into one
129
129
using ` AbortSignal .any ()` , which creates a _new signal_ that aborts when any of the given _cancellation signals_ abort.
130
130
It's similar to ` Promise .any ()` , but for ` AbortSignal` .
131
131
132
- A component can provide the more traditional ` start ()` and ` stop ()` APIs, as well allowing _cancellation signals_ to be
132
+ A component can offer the more traditional ` start ()` and ` stop ()` APIs, as well allowing _cancellation signals_ to be
133
133
passed via ` start ({ signal })` . Making use of internal and external _cancellation signals_, with ` AbortSignal .any ()` :
134
134
135
135
` ` ` js
0 commit comments