@@ -5,52 +5,117 @@ import { expectPromise } from '../../__testUtils__/expectPromise.js';
5
5
import { PromiseCanceller } from '../PromiseCanceller.js' ;
6
6
7
7
describe ( 'PromiseCanceller' , ( ) => {
8
- it ( 'works to cancel an already resolved promise' , async ( ) => {
9
- const abortController = new AbortController ( ) ;
10
- const abortSignal = abortController . signal ;
8
+ describe ( 'cancellablePromise' , ( ) => {
9
+ it ( 'works to cancel an already resolved promise' , async ( ) => {
10
+ const abortController = new AbortController ( ) ;
11
+ const abortSignal = abortController . signal ;
11
12
12
- const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
13
+ const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
13
14
14
- const promise = Promise . resolve ( 1 ) ;
15
+ const promise = Promise . resolve ( 1 ) ;
15
16
16
- const withCancellation = promiseCanceller . withCancellation ( promise ) ;
17
+ const withCancellation = promiseCanceller . cancellablePromise ( promise ) ;
17
18
18
- abortController . abort ( new Error ( 'Cancelled!' ) ) ;
19
+ abortController . abort ( new Error ( 'Cancelled!' ) ) ;
19
20
20
- await expectPromise ( withCancellation ) . toRejectWith ( 'Cancelled!' ) ;
21
- } ) ;
21
+ await expectPromise ( withCancellation ) . toRejectWith ( 'Cancelled!' ) ;
22
+ } ) ;
23
+
24
+ it ( 'works to cancel an already resolved promise after abort signal triggered' , async ( ) => {
25
+ const abortController = new AbortController ( ) ;
26
+ const abortSignal = abortController . signal ;
27
+
28
+ abortController . abort ( new Error ( 'Cancelled!' ) ) ;
22
29
23
- it ( 'works to cancel a hanging promise' , async ( ) => {
24
- const abortController = new AbortController ( ) ;
25
- const abortSignal = abortController . signal ;
30
+ const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
26
31
27
- const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
32
+ const promise = Promise . resolve ( 1 ) ;
28
33
29
- const promise = new Promise ( ( ) => {
30
- /* never resolves */
34
+ const withCancellation = promiseCanceller . cancellablePromise ( promise ) ;
35
+
36
+ await expectPromise ( withCancellation ) . toRejectWith ( 'Cancelled!' ) ;
31
37
} ) ;
32
38
33
- const withCancellation = promiseCanceller . withCancellation ( promise ) ;
39
+ it ( 'works to cancel a hanging promise' , async ( ) => {
40
+ const abortController = new AbortController ( ) ;
41
+ const abortSignal = abortController . signal ;
42
+
43
+ const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
44
+
45
+ const promise = new Promise ( ( ) => {
46
+ /* never resolves */
47
+ } ) ;
48
+
49
+ const withCancellation = promiseCanceller . cancellablePromise ( promise ) ;
50
+
51
+ abortController . abort ( new Error ( 'Cancelled!' ) ) ;
52
+
53
+ await expectPromise ( withCancellation ) . toRejectWith ( 'Cancelled!' ) ;
54
+ } ) ;
55
+
56
+ it ( 'works to cancel a hanging promise created after abort signal triggered' , async ( ) => {
57
+ const abortController = new AbortController ( ) ;
58
+ const abortSignal = abortController . signal ;
59
+
60
+ abortController . abort ( new Error ( 'Cancelled!' ) ) ;
34
61
35
- abortController . abort ( new Error ( 'Cancelled!' ) ) ;
62
+ const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
36
63
37
- await expectPromise ( withCancellation ) . toRejectWith ( 'Cancelled!' ) ;
64
+ const promise = new Promise ( ( ) => {
65
+ /* never resolves */
66
+ } ) ;
67
+
68
+ const withCancellation = promiseCanceller . cancellablePromise ( promise ) ;
69
+
70
+ await expectPromise ( withCancellation ) . toRejectWith ( 'Cancelled!' ) ;
71
+ } ) ;
38
72
} ) ;
39
73
40
- it ( 'works to cancel a hanging promise created after abort signal triggered' , async ( ) => {
41
- const abortController = new AbortController ( ) ;
42
- const abortSignal = abortController . signal ;
74
+ describe ( 'cancellableAsyncIterable' , ( ) => {
75
+ it ( 'works to abort a next call' , async ( ) => {
76
+ const abortController = new AbortController ( ) ;
77
+ const abortSignal = abortController . signal ;
78
+
79
+ const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
80
+
81
+ const asyncIterable = {
82
+ [ Symbol . asyncIterator ] : ( ) => ( {
83
+ next : ( ) => Promise . resolve ( { value : 1 , done : false } ) ,
84
+ } ) ,
85
+ } ;
86
+
87
+ const cancellableAsyncIterable =
88
+ promiseCanceller . cancellableIterable ( asyncIterable ) ;
43
89
44
- abortController . abort ( new Error ( 'Cancelled!' ) ) ;
90
+ const nextPromise =
91
+ cancellableAsyncIterable [ Symbol . asyncIterator ] ( ) . next ( ) ;
45
92
46
- const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
93
+ abortController . abort ( new Error ( 'Cancelled!' ) ) ;
47
94
48
- const promise = new Promise ( ( ) => {
49
- /* never resolves */
95
+ await expectPromise ( nextPromise ) . toRejectWith ( 'Cancelled!' ) ;
50
96
} ) ;
51
97
52
- const withCancellation = promiseCanceller . withCancellation ( promise ) ;
98
+ it ( 'works to abort a next call when already aborted' , async ( ) => {
99
+ const abortController = new AbortController ( ) ;
100
+ const abortSignal = abortController . signal ;
53
101
54
- await expectPromise ( withCancellation ) . toRejectWith ( 'Cancelled!' ) ;
102
+ abortController . abort ( new Error ( 'Cancelled!' ) ) ;
103
+
104
+ const promiseCanceller = new PromiseCanceller ( abortSignal ) ;
105
+
106
+ const asyncIterable = {
107
+ [ Symbol . asyncIterator ] : ( ) => ( {
108
+ next : ( ) => Promise . resolve ( { value : 1 , done : false } ) ,
109
+ } ) ,
110
+ } ;
111
+
112
+ const cancellableAsyncIterable =
113
+ promiseCanceller . cancellableIterable ( asyncIterable ) ;
114
+
115
+ const nextPromise =
116
+ cancellableAsyncIterable [ Symbol . asyncIterator ] ( ) . next ( ) ;
117
+
118
+ await expectPromise ( nextPromise ) . toRejectWith ( 'Cancelled!' ) ;
119
+ } ) ;
55
120
} ) ;
56
121
} ) ;
0 commit comments