| OLD | NEW |
| 1 --- | 1 --- |
| 2 layout: article | 2 layout: article |
| 3 title: "The Event Loop and Dart" | 3 title: "The Event Loop and Dart" |
| 4 description: "Learn how Dart handles the event queue and microtask queue, so you
can write better asynchronous code with fewer surprises." | 4 description: "Learn how Dart handles the event queue and microtask queue, so you
can write better asynchronous code with fewer surprises." |
| 5 rel: | 5 rel: |
| 6 author: kathy-walrath | 6 author: kathy-walrath |
| 7 has-permalinks: true | 7 has-permalinks: true |
| 8 article: | 8 article: |
| 9 written_on: 2013-09-30 | 9 written_on: 2013-09-30 |
| 10 updated_on: 2013-10-22 | 10 updated_on: 2013-10-22 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 If you can’t, then use scheduleMicrotask() to | 239 If you can’t, then use scheduleMicrotask() to |
| 240 add an item to the microtask queue. | 240 add an item to the microtask queue. |
| 241 For example, in a web app use a microtask to | 241 For example, in a web app use a microtask to |
| 242 avoid prematurely releasing a js-interop proxy or | 242 avoid prematurely releasing a js-interop proxy or |
| 243 ending an IndexedDB transaction or event handler. | 243 ending an IndexedDB transaction or event handler. |
| 244 | 244 |
| 245  | 245  |
| 246 | 246 |
| 247 | 247 |
| 248 #### Event queue: new Future() | 248 #### Event queue: new Future() |
| 249 {:.no_toc} |
| 249 | 250 |
| 250 To schedule a task on the event queue, | 251 To schedule a task on the event queue, |
| 251 use `new Future()` or `new Future.delayed()`. | 252 use `new Future()` or `new Future.delayed()`. |
| 252 These are two of the | 253 These are two of the |
| 253 [Future](http://api.dartlang.org/dart_async/Future.html) | 254 [Future](http://api.dartlang.org/dart_async/Future.html) |
| 254 constructors defined in the dart:async library. | 255 constructors defined in the dart:async library. |
| 255 | 256 |
| 256 <aside class="alert alert-info" markdown="1"> | 257 <aside class="alert alert-info" markdown="1"> |
| 257 **Note:** | 258 **Note:** |
| 258 You can also use | 259 You can also use |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 1. The **Future()** and **Future.delayed()** constructors | 325 1. The **Future()** and **Future.delayed()** constructors |
| 325 don’t complete immediately; | 326 don’t complete immediately; |
| 326 they add an item to the event queue. | 327 they add an item to the event queue. |
| 327 1. The **Future.value()** constructor completes in a microtask, | 328 1. The **Future.value()** constructor completes in a microtask, |
| 328 similar to #2. | 329 similar to #2. |
| 329 1. The **Future.sync()** constructor executes its function argument immediately | 330 1. The **Future.sync()** constructor executes its function argument immediately |
| 330 and (unless that function returns a Future) | 331 and (unless that function returns a Future) |
| 331 completes in a microtask, similar to #2. | 332 completes in a microtask, similar to #2. |
| 332 | 333 |
| 333 #### Microtask queue: scheduleMicrotask() | 334 #### Microtask queue: scheduleMicrotask() |
| 335 {:.no_toc} |
| 334 | 336 |
| 335 The dart:async library defines scheduleMicrotask() as a top-level function. | 337 The dart:async library defines scheduleMicrotask() as a top-level function. |
| 336 You can call scheduleMicrotask() like this: | 338 You can call scheduleMicrotask() like this: |
| 337 | 339 |
| 338 {% prettify dart %} | 340 {% prettify dart %} |
| 339 scheduleMicrotask(() { | 341 scheduleMicrotask(() { |
| 340 // ...code goes here... | 342 // ...code goes here... |
| 341 }); | 343 }); |
| 342 {% endprettify %} | 344 {% endprettify %} |
| 343 | 345 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 The reason is that returning a Future from your callback | 554 The reason is that returning a Future from your callback |
| 553 is how you get then() (which itself returns a new Future) | 555 is how you get then() (which itself returns a new Future) |
| 554 to _chain_ those two Futures together, | 556 to _chain_ those two Futures together, |
| 555 so that the Future returned by then() completes | 557 so that the Future returned by then() completes |
| 556 when the Future returned by the callback completes. | 558 when the Future returned by the callback completes. |
| 557 See the [then() reference](http://api.dartlang.org/docs/releases/latest/dart_asy
nc/Future.html#then) | 559 See the [then() reference](http://api.dartlang.org/docs/releases/latest/dart_asy
nc/Future.html#then) |
| 558 for more information. | 560 for more information. |
| 559 | 561 |
| 560 | 562 |
| 561 #### Annotated sample and output | 563 #### Annotated sample and output |
| 564 {:.no_toc} |
| 562 | 565 |
| 563 Here are some figures that might clarify the answer to question #2. | 566 Here are some figures that might clarify the answer to question #2. |
| 564 First, here’s the annotated program source: | 567 First, here’s the annotated program source: |
| 565 | 568 |
| 566  | 569  |
| 567 | 570 |
| 568 And here’s what the queues and output look like at various points in time, | 571 And here’s what the queues and output look like at various points in time, |
| 569 assuming no external events come in: | 572 assuming no external events come in: |
| 570 | 573 |
| 571  | 574  |
| (...skipping 29 matching lines...) Expand all Loading... |
| 601 avoid compute-intensive tasks on either event loop. | 604 avoid compute-intensive tasks on either event loop. |
| 602 * To perform compute-intensive tasks, | 605 * To perform compute-intensive tasks, |
| 603 create additional isolates or workers. | 606 create additional isolates or workers. |
| 604 | 607 |
| 605 As you write asynchronous code, you might find these resources helpful: | 608 As you write asynchronous code, you might find these resources helpful: |
| 606 | 609 |
| 607 * [Using Future Based APIs](/articles/using-future-based-apis/) | 610 * [Using Future Based APIs](/articles/using-future-based-apis/) |
| 608 * [Futures and Error Handling](/articles/futures-and-error-handling/) | 611 * [Futures and Error Handling](/articles/futures-and-error-handling/) |
| 609 * [dart:async - Asynchronous Programming](/docs/dart-up-and-running/contents/ch0
3.html#ch03-asynchronous-programming) section of the library tour | 612 * [dart:async - Asynchronous Programming](/docs/dart-up-and-running/contents/ch0
3.html#ch03-asynchronous-programming) section of the library tour |
| 610 * [dart:async API reference](http://api.dartlang.org/dart_async.html) | 613 * [dart:async API reference](http://api.dartlang.org/dart_async.html) |
| OLD | NEW |