94
94
95
95
pub fn all_of ( promises : Vec < Rc < Self > > ) -> Rc < Promise < Vec < T > > > {
96
96
let next_promise = Promise :: new ( ) ;
97
+
98
+ if promises. is_empty ( ) {
99
+ next_promise. fulfill ( vec ! [ ] ) ;
100
+ return next_promise;
101
+ }
102
+
97
103
let total = promises. len ( ) ;
98
104
let results = Rc :: new ( RefCell :: new ( vec ! [ None ; total] ) ) ;
99
105
let remaining = Rc :: new ( RefCell :: new ( total) ) ;
@@ -186,41 +192,59 @@ mod tests {
186
192
187
193
#[ test]
188
194
fn test_promise_fulfill ( ) {
195
+ let touched = Rc :: new ( RefCell :: new ( false ) ) ;
196
+ let touched_clone = touched. clone ( ) ;
197
+
189
198
let promise = Promise :: < i32 > :: new ( ) ;
190
- let next_promise = promise. then ( |result| {
199
+ let _next_promise = promise. then ( move |result| {
191
200
assert_eq ! ( result, 42 ) ;
201
+ * touched_clone. borrow_mut ( ) = true ;
192
202
} ) ;
193
203
194
204
promise. fulfill ( 42 ) ;
205
+ assert_eq ! ( true , touched. take( ) )
195
206
}
196
207
197
208
#[ test]
198
209
fn test_promise_reject ( ) {
210
+ let touched = Rc :: new ( RefCell :: new ( false ) ) ;
211
+ let touched_clone = touched. clone ( ) ;
212
+
199
213
let promise = Promise :: < String > :: new ( ) ;
200
- let next_promise = promise. catch ( |err| {
214
+ let _next_promise = promise. catch ( move |err| {
201
215
assert_eq ! ( err, "Error" ) ;
216
+ * touched_clone. borrow_mut ( ) = true ;
202
217
} ) ;
203
218
204
219
promise. reject ( "Error" . to_string ( ) ) ;
220
+ assert_eq ! ( true , touched. take( ) )
205
221
}
206
222
207
223
#[ test]
208
224
fn test_promise_chain ( ) {
225
+ let touched = Rc :: new ( RefCell :: new ( false ) ) ;
226
+ let touched_clone = touched. clone ( ) ;
227
+
209
228
let promise = Promise :: < i32 > :: new ( ) ;
210
229
let next_promise = promise. then ( |result| {
211
230
assert_eq ! ( result, 10 ) ;
212
231
20
213
232
} ) ;
214
233
215
- next_promise. then ( |result| {
234
+ next_promise. then ( move |result| {
216
235
assert_eq ! ( result, 20 ) ;
236
+ * touched_clone. borrow_mut ( ) = true ;
217
237
} ) ;
218
238
219
239
promise. fulfill ( 10 ) ;
240
+ assert_eq ! ( true , touched. take( ) )
220
241
}
221
242
222
243
#[ test]
223
244
fn test_all_of_success ( ) {
245
+ let touched = Rc :: new ( RefCell :: new ( false ) ) ;
246
+ let touched_clone = touched. clone ( ) ;
247
+
224
248
let promise1 = Promise :: < i32 > :: new ( ) ;
225
249
let promise2 = Promise :: < i32 > :: new ( ) ;
226
250
@@ -230,18 +254,24 @@ mod tests {
230
254
promise2. fulfill ( 100 ) ;
231
255
232
256
all_promise
233
- . then ( |results| {
257
+ . then ( move |results| {
234
258
assert_eq ! ( results. len( ) , 2 ) ;
235
259
assert_eq ! ( results[ 0 ] , 42 ) ;
236
260
assert_eq ! ( results[ 1 ] , 100 ) ;
261
+ * touched_clone. borrow_mut ( ) = true ;
237
262
} )
238
263
. catch ( |_err| {
239
264
panic ! ( "Should not reach here" ) ;
240
265
} ) ;
266
+
267
+ assert_eq ! ( true , touched. take( ) )
241
268
}
242
269
243
270
#[ test]
244
271
fn test_all_of_failure ( ) {
272
+ let touched = Rc :: new ( RefCell :: new ( false ) ) ;
273
+ let touched_clone = touched. clone ( ) ;
274
+
245
275
let promise1 = Promise :: < i32 > :: new ( ) ;
246
276
let promise2 = Promise :: < i32 > :: new ( ) ;
247
277
@@ -254,13 +284,19 @@ mod tests {
254
284
. then ( |_results| {
255
285
panic ! ( "Should not reach here" ) ;
256
286
} )
257
- . catch ( |err| {
287
+ . catch ( move |err| {
258
288
assert_eq ! ( err, "Error 1" ) ;
289
+ * touched_clone. borrow_mut ( ) = true ;
259
290
} ) ;
291
+
292
+ assert_eq ! ( true , touched. take( ) )
260
293
}
261
294
262
295
#[ test]
263
296
fn test_all_of_mixed_results ( ) {
297
+ let touched = Rc :: new ( RefCell :: new ( false ) ) ;
298
+ let touched_clone = touched. clone ( ) ;
299
+
264
300
let promise1 = Promise :: < i32 > :: new ( ) ;
265
301
let promise2 = Promise :: < i32 > :: new ( ) ;
266
302
@@ -273,19 +309,30 @@ mod tests {
273
309
. then ( |_| {
274
310
panic ! ( "Should not reach here" ) ;
275
311
} )
276
- . catch ( |reason| assert_eq ! ( reason, "Error" . to_string( ) ) ) ;
312
+ . catch ( move |reason| {
313
+ assert_eq ! ( reason, "Error" . to_string( ) ) ;
314
+ * touched_clone. borrow_mut ( ) = true ;
315
+ } ) ;
316
+
317
+ assert_eq ! ( true , touched. take( ) )
277
318
}
278
319
279
320
#[ test]
280
321
fn test_all_of_empty ( ) {
322
+ let touched = Rc :: new ( RefCell :: new ( false ) ) ;
323
+ let touched_clone = touched. clone ( ) ;
324
+
281
325
let all_promise = Promise :: < i32 > :: all_of ( vec ! [ ] ) ;
282
326
283
327
all_promise
284
- . then ( |results| {
328
+ . then ( move |results| {
285
329
assert ! ( results. is_empty( ) ) ;
330
+ * touched_clone. borrow_mut ( ) = true ;
286
331
} )
287
332
. catch ( |_err| {
288
333
panic ! ( "Should not reach here" ) ;
289
334
} ) ;
335
+
336
+ assert_eq ! ( true , touched. take( ) )
290
337
}
291
338
}
0 commit comments