|
215 | 215 | * // D
|
216 | 216 | * // fin
|
217 | 217 | *
|
| 218 | + * Callbacks attached to an _unresolved_ promise within a task function are |
| 219 | + * only weakly scheduled as subtasks and will be dropped if they reach the |
| 220 | + * front of the queue before the promise is resolved. In the example below, the |
| 221 | + * callbacks for `B` & `D` are dropped as sub-tasks since they are attached to |
| 222 | + * an unresolved promise when they reach the front of the task queue. |
| 223 | + * |
| 224 | + * var d = promise.defer(); |
| 225 | + * flow.execute(function() { |
| 226 | + * flow.execute( () => console.log('A')); |
| 227 | + * d.promise.then(() => console.log('B')); |
| 228 | + * flow.execute( () => console.log('C')); |
| 229 | + * d.promise.then(() => console.log('D')); |
| 230 | + * |
| 231 | + * setTimeout(d.fulfill, 20); |
| 232 | + * }).then(function() { |
| 233 | + * console.log('fin') |
| 234 | + * }); |
| 235 | + * // A |
| 236 | + * // C |
| 237 | + * // fin |
| 238 | + * // B |
| 239 | + * // D |
| 240 | + * |
218 | 241 | * If a promise is resolved while a task function is on the call stack, any
|
219 |
| - * previously registered callbacks (i.e. attached while the task was _not_ on |
220 |
| - * the call stack), act as _interrupts_ and are inserted at the front of the |
221 |
| - * task queue. If multiple promises are fulfilled, their interrupts are enqueued |
222 |
| - * in the order the promises are resolved. |
| 242 | + * previously registered and unqueued callbacks (i.e. either attached while no |
| 243 | + * task was on the call stack, or previously dropped as described above) act as |
| 244 | + * _interrupts_ and are inserted at the front of the task queue. If multiple |
| 245 | + * promises are fulfilled, their interrupts are enqueued in the order the |
| 246 | + * promises are resolved. |
223 | 247 | *
|
224 | 248 | * var d1 = promise.defer();
|
225 | 249 | * d1.promise.then(() => console.log('A'));
|
|
228 | 252 | * d2.promise.then(() => console.log('B'));
|
229 | 253 | *
|
230 | 254 | * flow.execute(function() {
|
231 |
| - * flow.execute(() => console.log('C')); |
| 255 | + * d1.promise.then(() => console.log('C')); |
232 | 256 | * flow.execute(() => console.log('D'));
|
| 257 | + * }); |
| 258 | + * flow.execute(function() { |
| 259 | + * flow.execute(() => console.log('E')); |
| 260 | + * flow.execute(() => console.log('F')); |
233 | 261 | * d1.fulfill();
|
234 | 262 | * d2.fulfill();
|
235 | 263 | * }).then(function() {
|
236 | 264 | * console.log('fin');
|
237 | 265 | * });
|
| 266 | + * // D |
238 | 267 | * // A
|
239 |
| - * // B |
240 | 268 | * // C
|
241 |
| - * // D |
| 269 | + * // B |
| 270 | + * // E |
| 271 | + * // F |
242 | 272 | * // fin
|
243 | 273 | *
|
244 | 274 | * Within a task function (or callback), each step of a promise chain acts as
|
|
321 | 351 | * // F
|
322 | 352 | * // fin
|
323 | 353 | *
|
324 |
| - * __Note__: while the ControlFlow will wait for |
325 |
| - * {@linkplain webdriver.promise.ControlFlow#execute tasks} and |
326 |
| - * {@linkplain webdriver.promise.Promise#then callbacks} to complete, it |
327 |
| - * _will not_ wait for unresolved promises created within a task: |
328 |
| - * |
329 |
| - * flow.execute(function() { |
330 |
| - * var p = new promise.Promise(function(fulfill) { |
331 |
| - * setTimeout(fulfill, 100); |
332 |
| - * }); |
333 |
| - * p.then(() => console.log('promise resolved!')); |
334 |
| - * flow.execute(() => console.log('sub-task!')); |
335 |
| - * }).then(function() { |
336 |
| - * console.log('task complete!'); |
337 |
| - * }); |
338 |
| - * // sub-task! |
339 |
| - * // task complete! |
340 |
| - * // promise resolved! |
341 |
| - * |
342 | 354 | * Finally, consider the following:
|
343 | 355 | *
|
344 | 356 | * var d = promise.defer();
|
@@ -2628,9 +2640,13 @@ var TaskQueue = goog.defineClass(EventEmitter, {
|
2628 | 2640 | return;
|
2629 | 2641 | }
|
2630 | 2642 | promise.callbacks_.forEach(function(cb) {
|
| 2643 | + cb.blocked = false; |
| 2644 | + if (cb.queue) { |
| 2645 | + return; |
| 2646 | + } |
| 2647 | + |
2631 | 2648 | cb.promise[CANCEL_HANDLER_SYMBOL] = this.onTaskCancelled_.bind(this, cb);
|
2632 | 2649 |
|
2633 |
| - cb.blocked = false; |
2634 | 2650 | if (cb.queue === this && this.tasks_.indexOf(cb) !== -1) {
|
2635 | 2651 | return;
|
2636 | 2652 | }
|
|
0 commit comments