Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Support for asynchronous programming, | 6 * Support for asynchronous programming, |
| 7 * with classes such as Future and Stream. | 7 * with classes such as Future and Stream. |
| 8 * | 8 * |
| 9 * For an introduction to asynchronous programming in Dart, see the | 9 * Understanding Futures and Streams is a prerequisite for |
| 10 * [dart:async section of the language tour] | 10 * writing just about any Dart program. |
| 11 * (https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-as ynchronous-programming). | 11 * |
| 12 * To use this library in your code: | |
| 13 * | |
| 14 * import 'dart:async'; | |
| 15 * | |
| 16 * ## Future | |
| 17 * | |
| 18 * A Future object provides a way to perform an | |
| 19 * operation asynchronously so the program doesn't block | |
| 20 * waiting for a lengthy operation to complete. | |
|
Søren Gjesse
2013/12/19 09:44:40
This is not really true in this example. As each D
Lasse Reichstein Nielsen
2013/12/19 15:22:49
Agree.
I see a future as representing a computatio
mem
2013/12/19 23:24:24
Done.
| |
| 21 * Here's an example of using a Future to calculate the Fibonacci | |
| 22 * value for the number 50. | |
| 23 * | |
| 24 * new Future(() => fibonacci(50)) | |
| 25 * .then((fibValue) { print(fibValue); }) | |
|
Søren Gjesse
2013/12/19 09:44:40
Use the => syntax everywhere?
Lasse Reichstein Nielsen
2013/12/19 15:22:49
Or not even that; in this case, you can just write
| |
| 26 * .catchError((_) { print('An error occurred.'); }); | |
| 27 * | |
| 28 * [Future.then] registers a callback function that runs | |
| 29 * when the Future's operation completes successfully. | |
| 30 * The value returned by the operation | |
| 31 * is passed into the callback function. | |
| 32 * In this example, the fibonacci function returns a number, | |
| 33 * and the callback function prints that number. | |
| 34 * [Future.catchError] registers a callback function that | |
| 35 * runs if an error occurs within the Future. | |
| 36 * | |
| 37 * ## Stream | |
| 38 * | |
| 39 * A Stream provides an asynchronous sequence of data. | |
| 40 * Examples of data sequences include user-generated events, | |
| 41 * such as mouse clicks, and a stream of bytes read from a file. | |
| 42 * The following example opens a file for reading. | |
| 43 * [Stream.listen] registers a callback function that runs | |
| 44 * each time more data is available. | |
| 45 * | |
| 46 * Stream<List<int>> stream = new File('quotes.txt').openRead(); | |
| 47 * stream.transform(UTF8.decoder).listen((data) { | |
|
Søren Gjesse
2013/12/19 09:44:40
Shorten to
stream.transform(UTF8.decoder).listen(
mem
2013/12/19 23:24:24
Done.
| |
| 48 * print(data); | |
| 49 * }); | |
| 50 * | |
| 51 * The stream returns a list of bytes. | |
|
Søren Gjesse
2013/12/19 09:44:40
The stream emits a sequence of list of bytes (depe
mem
2013/12/19 23:24:24
Done.
| |
| 52 * The program must interpret the bytes or handle the raw byte data. | |
| 53 * Here, the code uses a UTF8 decoder (provided in the [dart:convert] library) | |
|
Lasse Reichstein Nielsen
2013/12/19 15:22:49
Does [dart:convert] work as a DartDoc link?
mem
2013/12/19 23:24:24
no but it should.
I was optimistic.
| |
| 54 * to convert the file's UTF8-encoded text data into a Dart string. | |
|
Søren Gjesse
2013/12/19 09:44:40
Maybe be more explicit on saying that the converte
mem
2013/12/19 23:24:24
Done.
| |
| 55 * | |
| 56 * Another common use of streams is for user-generated events | |
| 57 * in a web app: The following code listens for mouse clicks on a button. | |
| 58 * | |
| 59 * querySelector('#myButton').onClick.listen((_) { print('Click.'); } ); | |
| 12 * | 60 * |
| 13 * ## Other resources | 61 * ## Other resources |
| 14 * | 62 * |
| 15 * * [Using Future Based APIs] | 63 * * The [dart:async section of the library tour] |
| 16 * (https://www.dartlang.org/articles/using-future-based-apis/): A first look at | 64 * (https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-as ynchronous-programming): |
| 65 * A brief overview of asynchronous programming. | |
| 66 * | |
| 67 * * [Use Future-Based APIs] | |
| 68 * (https://www.dartlang.org/docs/tutorials/futures/): A closer look at | |
| 17 * Futures and how to use them to write asynchronous Dart code. | 69 * Futures and how to use them to write asynchronous Dart code. |
| 18 * | 70 * |
| 19 * * [Futures and Error Handling] | 71 * * [Futures and Error Handling] |
| 20 * (https://www.dartlang.org/articles/futures-and-error-handling/): Everything | 72 * (https://www.dartlang.org/articles/futures-and-error-handling/): Everything |
| 21 * you wanted to know about handling errors and exceptions when working with | 73 * you wanted to know about handling errors and exceptions when working with |
| 22 * Futures (but were afraid to ask). | 74 * Futures (but were afraid to ask). |
| 23 * | 75 * |
| 24 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): | 76 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): |
| 25 * Learn how Dart handles the event queue and microtask queue, so you can write | 77 * Learn how Dart handles the event queue and microtask queue, so you can write |
| 26 * better asynchronous code with fewer surprises. | 78 * better asynchronous code with fewer surprises. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 40 part 'future.dart'; | 92 part 'future.dart'; |
| 41 part 'future_impl.dart'; | 93 part 'future_impl.dart'; |
| 42 part 'schedule_microtask.dart'; | 94 part 'schedule_microtask.dart'; |
| 43 part 'stream.dart'; | 95 part 'stream.dart'; |
| 44 part 'stream_controller.dart'; | 96 part 'stream_controller.dart'; |
| 45 part 'stream_impl.dart'; | 97 part 'stream_impl.dart'; |
| 46 part 'stream_pipe.dart'; | 98 part 'stream_pipe.dart'; |
| 47 part 'stream_transformers.dart'; | 99 part 'stream_transformers.dart'; |
| 48 part 'timer.dart'; | 100 part 'timer.dart'; |
| 49 part 'zone.dart'; | 101 part 'zone.dart'; |
| OLD | NEW |