1 | ## CancelToken and Cancel Polyfill
|
2 |
|
3 | This a polyfill of the polyfillable parts of the proposed cancellable promises spec.
|
4 |
|
5 | See https://github.com/littledan/proposal-cancelable-promises for more details.
|
6 |
|
7 |
|
8 | ### Use
|
9 |
|
10 | ```javascript
|
11 | import {CancelToken} from 'cancel-token';
|
12 |
|
13 | const source = CancelToken.source();
|
14 |
|
15 | const result = someCancellableApi(source.token);
|
16 |
|
17 | eventSource.on('dont-care-about-result-anymore', () {
|
18 | source.cancel();
|
19 | });
|
20 |
|
21 | // This may throw a Cancel if we called source.cancel().
|
22 | const value = await result;
|
23 |
|
24 |
|
25 | // Elsewhere, maybe in another library
|
26 | async function someCancellableApi(cancelToken) {
|
27 | await doSomeWork(cancelToken);
|
28 |
|
29 | // I'm outside of a critical section here, so it's ok if I were to throw.
|
30 | cancelToken.throwIfRequested();
|
31 |
|
32 | return doSomeMoreWork(cancelToken);
|
33 | }
|
34 | ```
|
35 |
|
36 | ### Developing
|
37 |
|
38 | To test:
|
39 |
|
40 | npm test
|
41 |
|
42 | To watch source files and rerun tests when they change:
|
43 |
|
44 | npm test:watch
|