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 part of dart.async; | 5 part of dart.async; |
6 | 6 |
7 /** | 7 /** |
8 * A [Future] is used to obtain a value sometime in the future. Receivers of a | 8 * A [Future] is used to obtain a value sometime in the future. Receivers of a |
9 * [Future] can obtain the value by passing a callback to [then]. For example: | 9 * [Future] can obtain the value by passing a callback to [then]. For example: |
10 * | 10 * |
(...skipping 18 matching lines...) Expand all Loading... |
29 */ | 29 */ |
30 abstract class Future<T> { | 30 abstract class Future<T> { |
31 /** A future whose value is immediately available. */ | 31 /** A future whose value is immediately available. */ |
32 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value); | 32 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value); |
33 | 33 |
34 /** A future that completes with an error. */ | 34 /** A future that completes with an error. */ |
35 factory Future.immediateError(var error, [Object stackTrace]) { | 35 factory Future.immediateError(var error, [Object stackTrace]) { |
36 return new _FutureImpl<T>.immediateError(error, stackTrace); | 36 return new _FutureImpl<T>.immediateError(error, stackTrace); |
37 } | 37 } |
38 | 38 |
39 factory Future.delayed(int milliseconds, dynamic value()) { | 39 /** |
40 var completer = new Completer<T>(); | 40 * Creates a future that completes after a delay. |
41 new Timer(milliseconds, (_) => completer.complete(null)); | 41 * |
42 return completer.future.then((_) => value()); | 42 * The future will be completed after [milliseconds] have passed with |
| 43 * the result of calling [value]. |
| 44 * |
| 45 * If calling [value] throws, the created future will complete with the |
| 46 * error. |
| 47 */ |
| 48 factory Future.delayed(int milliseconds, T value()) { |
| 49 _FutureImpl<T> future = new _ThenFuture<dynamic, T>((_) => value()); |
| 50 new Timer(milliseconds, (_) => future._sendValue(null)); |
| 51 return future; |
43 } | 52 } |
44 | 53 |
45 // TODO(floitsch): I don't think the typing is right here. | 54 /** |
46 // Otherwise new Future<int>.wait(...) would be a Future<List<int>>. Sounds | 55 * Wait for all the given futures to complete and collect their values. |
47 // wrong. | 56 * |
48 factory Future.wait(List<Future> futures) | 57 * Returns a future which will complete once all the futures in a list are |
49 => new _FutureImpl<List<T>>.wait(futures); | 58 * complete. If any of the futures in the list completes with an exception, |
| 59 * the resulting future also completes with an exception. Otherwise the value |
| 60 * of the returned future will be a list of all the values that were produced. |
| 61 */ |
| 62 static Future<List> wait(Iterable<Future> futures) { |
| 63 return new _FutureImpl<List>.wait(futures); |
| 64 } |
| 65 |
| 66 /** |
| 67 * Perform an async operation for each element of the iterable, in turn. |
| 68 * |
| 69 * Runs [f] for each element in [input] in order, moving to the next element |
| 70 * only when the [Future] returned by [f] completes. Returns a [Future] that |
| 71 * completes when all elements have been processed. |
| 72 * |
| 73 * The return values of all [Future]s are discarded. Any errors will cause the |
| 74 * iteration to stop and will be piped through the returned [Future]. |
| 75 */ |
| 76 static Future forEach(Iterable input, Future f(element)) { |
| 77 _FutureImpl doneSignal = new _FutureImpl(); |
| 78 Iterator iterator = input.iterator; |
| 79 void nextElement(_) { |
| 80 if (iterator.moveNext()) { |
| 81 f(iterator.current).then(nextElement, onError: doneSignal._setError); |
| 82 } else { |
| 83 doneSignal._setValue(null); |
| 84 } |
| 85 } |
| 86 nextElement(null); |
| 87 return doneSignal; |
| 88 } |
50 | 89 |
51 /** | 90 /** |
52 * When this future completes with a value, then [onValue] is called with this | 91 * When this future completes with a value, then [onValue] is called with this |
53 * value. If [this] future is already completed then the invocation of | 92 * value. If [this] future is already completed then the invocation of |
54 * [onValue] is delayed until the next event-loop iteration. | 93 * [onValue] is delayed until the next event-loop iteration. |
55 * | 94 * |
56 * Returns a new [Future] [:f:]. | 95 * Returns a new [Future] [:f:]. |
57 * | 96 * |
58 * If [this] is completed with an error then [:f:] is completed with the same | 97 * If [this] is completed with an error then [:f:] is completed with the same |
59 * error. If [this] is completed with a value, then [:f:]'s completion value | 98 * error. If [this] is completed with a value, then [:f:]'s completion value |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 * The argument [exception] should not be [:null:]. | 213 * The argument [exception] should not be [:null:]. |
175 * | 214 * |
176 * If [exception] is an [AsyncError], it is used directly as the error | 215 * If [exception] is an [AsyncError], it is used directly as the error |
177 * message sent to the future's listeners, and [stackTrace] is ignored. | 216 * message sent to the future's listeners, and [stackTrace] is ignored. |
178 * | 217 * |
179 * Otherwise the [exception] and an optional [stackTrace] is combined into an | 218 * Otherwise the [exception] and an optional [stackTrace] is combined into an |
180 * [AsyncError] and sent to this future's listeners. | 219 * [AsyncError] and sent to this future's listeners. |
181 */ | 220 */ |
182 void completeError(Object exception, [Object stackTrace]); | 221 void completeError(Object exception, [Object stackTrace]); |
183 } | 222 } |
184 | |
185 class Futures { | |
186 /** | |
187 * Returns a future which will complete once all the futures in a list are | |
188 * complete. If any of the futures in the list completes with an exception, | |
189 * the resulting future also completes with an exception. (The value of the | |
190 * returned future will be a list of all the values that were produced.) | |
191 */ | |
192 static Future<List> wait(Iterable<Future> futures) { | |
193 return new _FutureImpl<List>.wait(futures); | |
194 } | |
195 | |
196 /** | |
197 * Runs [f] for each element in [input] in order, moving to the next element | |
198 * only when the [Future] returned by [f] completes. Returns a [Future] that | |
199 * completes when all elements have been processed. | |
200 * | |
201 * The return values of all [Future]s are discarded. Any errors will cause the | |
202 * iteration to stop and will be piped through the returned [Future]. | |
203 */ | |
204 static Future forEach(Iterable input, Future f(element)) { | |
205 var iterator = input.iterator; | |
206 Future nextElement(_) { | |
207 if (!iterator.moveNext()) return new Future.immediate(null); | |
208 return f(iterator.current).then(nextElement); | |
209 } | |
210 return nextElement(null); | |
211 } | |
212 } | |
OLD | NEW |