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 * Most programs need to use [Future]s and [Stream]s, which |
Kathy Walrath
2013/12/16 21:18:48
I'd delete "which are often used together".
mem
2013/12/16 22:18:55
Done.
| |
10 * [dart:async section of the language tour] | 10 * are often used together, to perform asynchronous operations |
11 * (https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-as ynchronous-programming). | 11 * such as I/O. |
12 * Understanding Futures and Streams is a pre-requeiste for | |
Kathy Walrath
2013/12/16 21:18:48
requeiste: sp
I'd combine the previous paragraph
mem
2013/12/16 22:18:55
Done.
| |
13 * writing just about any Dart program. | |
14 * | |
15 * To use this library in your code: | |
16 * | |
17 * import 'dart:async'; | |
18 * | |
19 * ## Future | |
20 * | |
21 * A Future object provides a way to perform an | |
22 * operation asynchronously so the program doesn't block | |
23 * waiting for a lengthy operation to complete. | |
24 * Here's an example of using a Future to calculate the Fibonacci | |
25 * value for the number 50. | |
26 * | |
27 * new Future(() => fibonacci(50)) | |
28 * .then((fibValue) { print(fibValue); }) | |
29 * .catchError((_) { print('An error occurred.'); }); | |
30 * | |
31 * [Future.then] registers a callback function that runs | |
32 * when the Future's operation completes successfully. | |
33 * The value returned by the operation | |
34 * is passed into the callback function. | |
35 * In this example, the fibonacci function returns a number | |
36 * that is printed. | |
Kathy Walrath
2013/12/16 21:18:48
I'd split this up. ("that is printed" confused me)
mem
2013/12/16 22:18:55
Done.
| |
37 * [Future.catchError] registers a callback function that | |
38 * runs if an error occurs within the Future. | |
39 * | |
40 * ## Stream | |
41 * | |
42 * A Stream provides an asynchronous sequence of data. | |
43 * Examples of data sequences include user-generated events such as mouse clicks | |
44 * and a stream of bytes read from a file. | |
Kathy Walrath
2013/12/16 21:18:48
Put commas around "such as mouse clicks" so it's c
mem
2013/12/16 22:18:55
Done.
| |
45 * The following example opens a file for reading. | |
46 * [Stream.listen] registers a callback function that runs | |
47 * each time more data is available. | |
48 * | |
49 * Stream<List<int>> stream = new File('quotes.txt').openRead(); | |
50 * stream.transform(UTF8.decoder).listen((data) { | |
51 * print(data); | |
52 * }); | |
53 * | |
54 * The stream returns a list of bytes. | |
55 * The program must interpret the bytes or handle the raw byte data. | |
56 * Here, the code uses an UTF8 decoder (provided in the [dart:convert] library) | |
Kathy Walrath
2013/12/16 21:18:48
an UTF8 -> a UTF8
mem
2013/12/16 22:18:55
Done.
| |
57 * because the file is a simple text file. | |
Kathy Walrath
2013/12/16 21:18:48
the "because" didn't make sense to me.
Maybe:
to
mem
2013/12/16 22:18:55
Done.
| |
58 * | |
59 * Another common use of streams is for user-generated events | |
60 * in a web app. This code listens for mouse clicks on a button. | |
Kathy Walrath
2013/12/16 21:18:48
This -> The following
AND/OR use a : instead of a
mem
2013/12/16 22:18:55
Done.
| |
61 * | |
62 * querySelector('#myButton').onClick.listen((_) { print('Click.'); } ); | |
12 * | 63 * |
13 * ## Other resources | 64 * ## Other resources |
14 * | 65 * |
15 * * [Using Future Based APIs] | 66 * * For a brief overview to asynchronous programming in Dart, see the |
Kathy Walrath
2013/12/16 21:18:48
to -> of
Rewrite to match the other resources (Ti
mem
2013/12/16 22:18:55
Done.
| |
16 * (https://www.dartlang.org/articles/using-future-based-apis/): A first look at | 67 * [dart:async section of the library tour] |
68 * (https://www.dartlang.org/docs/dart-up-and-running/contents/ch03.html#ch03-as ynchronous-programming). | |
69 * | |
70 * * [Using Future-Based APIs] | |
Kathy Walrath
2013/12/16 21:18:48
Using -> Use
mem
2013/12/16 22:18:55
Done.
| |
71 * (https://www.dartlang.org/docs/tutorials/futures/): A closer look at | |
17 * Futures and how to use them to write asynchronous Dart code. | 72 * Futures and how to use them to write asynchronous Dart code. |
18 * | 73 * |
19 * * [Futures and Error Handling] | 74 * * [Futures and Error Handling] |
20 * (https://www.dartlang.org/articles/futures-and-error-handling/): Everything | 75 * (https://www.dartlang.org/articles/futures-and-error-handling/): Everything |
21 * you wanted to know about handling errors and exceptions when working with | 76 * you wanted to know about handling errors and exceptions when working with |
22 * Futures (but were afraid to ask). | 77 * Futures (but were afraid to ask). |
23 * | 78 * |
24 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): | 79 * * [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 | 80 * Learn how Dart handles the event queue and microtask queue, so you can write |
26 * better asynchronous code with fewer surprises. | 81 * better asynchronous code with fewer surprises. |
(...skipping 13 matching lines...) Expand all Loading... | |
40 part 'future.dart'; | 95 part 'future.dart'; |
41 part 'future_impl.dart'; | 96 part 'future_impl.dart'; |
42 part 'schedule_microtask.dart'; | 97 part 'schedule_microtask.dart'; |
43 part 'stream.dart'; | 98 part 'stream.dart'; |
44 part 'stream_controller.dart'; | 99 part 'stream_controller.dart'; |
45 part 'stream_impl.dart'; | 100 part 'stream_impl.dart'; |
46 part 'stream_pipe.dart'; | 101 part 'stream_pipe.dart'; |
47 part 'stream_transformers.dart'; | 102 part 'stream_transformers.dart'; |
48 part 'timer.dart'; | 103 part 'timer.dart'; |
49 part 'zone.dart'; | 104 part 'zone.dart'; |
OLD | NEW |