UNPKG

917 BMarkdownView Raw
1## CancelToken and Cancel Polyfill
2
3This a polyfill of the polyfillable parts of the proposed cancellable promises spec.
4
5See https://github.com/littledan/proposal-cancelable-promises for more details.
6
7
8### Use
9
10```javascript
11import {CancelToken} from 'cancel-token';
12
13const source = CancelToken.source();
14
15const result = someCancellableApi(source.token);
16
17eventSource.on('dont-care-about-result-anymore', () {
18 source.cancel();
19});
20
21// This may throw a Cancel if we called source.cancel().
22const value = await result;
23
24
25// Elsewhere, maybe in another library
26async 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
38To test:
39
40 npm test
41
42To watch source files and rerun tests when they change:
43
44 npm test:watch