@@ -147,16 +147,101 @@ describe('promise', function() {
147
147
return res ;
148
148
} ) ;
149
149
150
- it ( 'canCancelADeferredFromAChainedPromise' , function ( ) {
151
- var d = new promise . Deferred ( ) ;
152
- var p = d . promise . then ( assert . fail , function ( e ) {
153
- assert . ok ( e instanceof promise . CancellationError ) ;
154
- assert . equal ( 'because i said so' , e . message ) ;
150
+ describe ( 'can cancel original promise from its child;' , function ( ) {
151
+ it ( 'child created by then()' , function ( ) {
152
+ var d = new promise . Deferred ( ) ;
153
+ var p = d . promise . then ( assert . fail , function ( e ) {
154
+ assert . ok ( e instanceof promise . CancellationError ) ;
155
+ assert . equal ( 'because i said so' , e . message ) ;
156
+ return 123 ;
157
+ } ) ;
158
+
159
+ p . cancel ( 'because i said so' ) ;
160
+ return p . then ( v => assert . equal ( 123 , v ) ) ;
161
+ } ) ;
162
+
163
+ it ( 'child linked by resolving with parent' , function ( ) {
164
+ let parent = promise . defer ( ) ;
165
+ let child = new promise . Promise ( resolve => resolve ( parent . promise ) ) ;
166
+ child . cancel ( 'all done' ) ;
167
+
168
+ return parent . promise . then (
169
+ ( ) => assert . fail ( 'expected a cancellation' ) ,
170
+ e => {
171
+ assert . ok ( e instanceof promise . CancellationError ) ;
172
+ assert . equal ( 'all done' , e . message ) ;
173
+ } ) ;
174
+ } ) ;
175
+
176
+ it ( 'grand child through thenable chain' , function ( ) {
177
+ let p = new promise . Promise ( function ( ) { /* never resolve*/ } ) ;
178
+
179
+ let noop = function ( ) { } ;
180
+ let gc = p . then ( noop ) . then ( noop ) . then ( noop ) ;
181
+ gc . cancel ( 'stop!' ) ;
182
+
183
+ return p . then (
184
+ ( ) => assert . fail ( 'expected to be cancelled' ) ,
185
+ ( e ) => {
186
+ assert . ok ( e instanceof promise . CancellationError ) ;
187
+ assert . equal ( 'stop!' , e . message ) ;
188
+ } ) ;
189
+ } ) ;
190
+
191
+ it ( 'grand child through thenable chain started at resolve' , function ( ) {
192
+ function noop ( ) { }
193
+
194
+ let parent = promise . defer ( ) ;
195
+ let child = new promise . Promise ( resolve => resolve ( parent . promise ) ) ;
196
+ let grandChild = child . then ( noop ) . then ( noop ) . then ( noop ) ;
197
+ grandChild . cancel ( 'all done' ) ;
198
+
199
+ return parent . promise . then (
200
+ ( ) => assert . fail ( 'expected a cancellation' ) ,
201
+ e => {
202
+ assert . ok ( e instanceof promise . CancellationError ) ;
203
+ assert . equal ( 'all done' , e . message ) ;
204
+ } ) ;
155
205
} ) ;
156
- var p2 = p . then ( function ( ) { } , assert . fail ) ;
157
206
158
- p . cancel ( 'because i said so' ) ;
159
- return p2 ;
207
+ it ( '"parent" is a Thenable' , function ( ) {
208
+ function noop ( ) { }
209
+
210
+ class FakeThenable {
211
+ constructor ( p ) {
212
+ this . promise = p ;
213
+ }
214
+
215
+ cancel ( reason ) {
216
+ this . promise . cancel ( reason ) ;
217
+ }
218
+
219
+ then ( cb , eb ) {
220
+ let result = this . promise . then ( cb , eb ) ;
221
+ return new FakeThenable ( result ) ;
222
+ }
223
+ }
224
+ promise . Thenable . addImplementation ( FakeThenable ) ;
225
+
226
+ let root = new promise . Promise ( noop ) ;
227
+ let thenable = new FakeThenable ( root ) ;
228
+ assert . ok ( promise . Thenable . isImplementation ( thenable ) ) ;
229
+
230
+ let child = new promise . Promise ( resolve => resolve ( thenable ) ) ;
231
+ assert . ok ( child instanceof promise . Promise ) ;
232
+ child . cancel ( 'stop!' ) ;
233
+
234
+ function assertStopped ( p ) {
235
+ return p . then (
236
+ ( ) => assert . fail ( 'not stopped!' ) ,
237
+ ( e ) => {
238
+ assert . ok ( e instanceof promise . CancellationError ) ;
239
+ assert . equal ( 'stop!' , e . message ) ;
240
+ } ) ;
241
+ }
242
+
243
+ return assertStopped ( child ) . then ( ( ) => assertStopped ( root ) ) ;
244
+ } ) ;
160
245
} ) ;
161
246
162
247
it ( 'canCancelATimeout' , function ( ) {
@@ -167,6 +252,9 @@ describe('promise', function() {
167
252
return p ;
168
253
} ) ;
169
254
255
+ it ( 'can cancel timeout from grandchild' , function ( ) {
256
+ } ) ;
257
+
170
258
it ( 'cancelIsANoopOnceAPromiseHasBeenFulfilled' , function ( ) {
171
259
var p = promise . fulfilled ( 123 ) ;
172
260
p . cancel ( ) ;
@@ -214,28 +302,6 @@ describe('promise', function() {
214
302
d . fulfill ( 'hi' ) ;
215
303
return result . then ( callback . assertCalled ) ;
216
304
} ) ;
217
-
218
- it ( 'canCancelReturnedPromise' , function ( ) {
219
- var callbacks = callbackPair ( null , function ( e ) {
220
- assert . ok ( e instanceof promise . CancellationError ) ;
221
- assert . equal ( 'just because' , e . message ) ;
222
- } ) ;
223
-
224
- var promiseLike = {
225
- then : function ( cb , eb ) {
226
- this . callback = cb ;
227
- this . errback = eb ;
228
- }
229
- } ;
230
-
231
- var aPromise = promise . when ( promiseLike ,
232
- callbacks . callback , callbacks . errback ) ;
233
-
234
- assert . ok ( aPromise . isPending ( ) ) ;
235
- aPromise . cancel ( 'just because' ) ;
236
-
237
- return aPromise . finally ( callbacks . assertErrback ) ;
238
- } ) ;
239
305
} ) ;
240
306
241
307
it ( 'firesUncaughtExceptionEventIfRejectionNeverHandled' , function ( ) {
0 commit comments