Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: src/site/articles/event-loop/index.markdown

Issue 118633004: New TOC for dart-by-example page (Closed) Base URL: https://github.com/dart-lang/dartlang.org.git@master
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 ![shows chain of event handler execution, with tasks added using Future and sche duleMicrotask().](images/scheduling-tasks.png) 245 ![shows chain of event handler execution, with tasks added using Future and sche duleMicrotask().](images/scheduling-tasks.png)
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
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
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 ![Lines that schedule a microtask are gold; lines that schedule an event are blu e](images/test-annotated.png) 569 ![Lines that schedule a microtask are gold; lines that schedule an event are blu e](images/test-annotated.png)
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 ![3 columns: Time, Output, Queues](images/test-queue-output.png) 574 ![3 columns: Time, Output, Queues](images/test-queue-output.png)
(...skipping 29 matching lines...) Expand all
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)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698