OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /** | |
6 * Support for asynchronous programming, | |
7 * with classes such as Future and Stream. | |
8 * | |
9 * Understanding [Future]s and [Stream]s is a prerequisite for | |
10 * writing just about any Dart program. | |
11 * | |
12 * To use this library in your code: | |
13 * | |
14 * import 'dart:async'; | |
15 * | |
16 * ## Future | |
17 * | |
18 * A Future object represents a computation whose return value | |
19 * might not yet be available. | |
20 * The Future returns the value of the computation | |
21 * when it completes at some time in the future. | |
22 * Futures are often used for potentially lengthy computations | |
23 * such as I/O and interaction with users. | |
24 * | |
25 * Many methods in the Dart libraries return Futures when | |
26 * performing tasks. For example, when binding an HttpServer | |
27 * to a host and port, the `bind()` method returns a Future. | |
28 * | |
29 * HttpServer.bind('127.0.0.1', 4444) | |
30 * .then((server) => print('${server.isBroadcast}')) | |
31 * .catchError(print); | |
32 * | |
33 * [Future.then] registers a callback function that runs | |
34 * when the Future's operation, in this case the `bind()` method, | |
35 * completes successfully. | |
36 * The value returned by the operation | |
37 * is passed into the callback function. | |
38 * In this example, the `bind()` method returns the HttpServer | |
39 * object. The callback function prints one of its properties. | |
40 * [Future.catchError] registers a callback function that | |
41 * runs if an error occurs within the Future. | |
42 * | |
43 * ## Stream | |
44 * | |
45 * A Stream provides an asynchronous sequence of data. | |
46 * Examples of data sequences include individual events, like mouse clicks, | |
47 * or sequential chunks of larger data, like multiple byte lists with the | |
48 * contents of a file | |
49 * such as mouse clicks, and a stream of byte lists read from a file. | |
50 * The following example opens a file for reading. | |
51 * [Stream.listen] registers a callback function that runs | |
52 * each time more data is available. | |
53 * | |
54 * Stream<List<int>> stream = new File('quotes.txt').openRead(); | |
55 * stream.transform(UTF8.decoder).listen(print); | |
56 * | |
57 * The stream emits a sequence of a list of bytes. | |
58 * The program must interpret the bytes or handle the raw byte data. | |
59 * Here, the code uses a UTF8 decoder (provided in the `dart:convert` library) | |
60 * to convert the sequence of bytes into a sequence | |
61 * of Dart strings. | |
62 * | |
63 * Another common use of streams is for user-generated events | |
64 * in a web app: The following code listens for mouse clicks on a button. | |
65 * | |
66 * querySelector('#myButton').onClick.listen((_) => print('Click.')); | |
67 * | |
68 * ## Other resources | |
69 * | |
70 * * The [dart:async section of the library tour][asynchronous-programming]: | |
71 * A brief overview of asynchronous programming. | |
72 * | |
73 * * [Use Future-Based APIs][futures-tutorial]: A closer look at Futures and | |
74 * how to use them to write asynchronous Dart code. | |
75 * | |
76 * * [Futures and Error Handling][futures-error-handling]: Everything you | |
77 * wanted to know about handling errors and exceptions when working with | |
78 * Futures (but were afraid to ask). | |
79 * | |
80 * * [The Event Loop and Dart](https://www.dartlang.org/articles/event-loop/): | |
81 * Learn how Dart handles the event queue and microtask queue, so you can | |
82 * write better asynchronous code with fewer surprises. | |
83 * | |
84 * * [test package: Asynchronous Tests][test-readme]: How to test asynchronous | |
85 * code. | |
86 * | |
87 * [asynchronous-programming]: https://www.dartlang.org/docs/dart-up-and-running
/ch03.html#dartasync---asynchronous-programming | |
88 * [futures-tutorial]: https://www.dartlang.org/docs/tutorials/futures/ | |
89 * [futures-error-handling]: https://www.dartlang.org/articles/futures-and-error
-handling/ | |
90 * [test-readme]: https://pub.dartlang.org/packages/test | |
91 */ | |
92 library dart.async; | |
93 | |
94 import "dart:collection"; | |
95 import "dart:_internal" show printToZone, printToConsole, | |
96 IterableElementError; | |
97 | |
98 part 'async_error.dart'; | |
99 part 'broadcast_stream_controller.dart'; | |
100 part 'deferred_load.dart'; | |
101 part 'future.dart'; | |
102 part 'future_impl.dart'; | |
103 part 'schedule_microtask.dart'; | |
104 part 'stream.dart'; | |
105 part 'stream_controller.dart'; | |
106 part 'stream_impl.dart'; | |
107 part 'stream_pipe.dart'; | |
108 part 'stream_transformers.dart'; | |
109 part 'timer.dart'; | |
110 part 'zone.dart'; | |
OLD | NEW |