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 library utils; | 5 library utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 | 192 |
193 int get hashCode => first.hashCode ^ last.hashCode; | 193 int get hashCode => first.hashCode ^ last.hashCode; |
194 } | 194 } |
195 | 195 |
196 /// Configures [future] so that its result (success or exception) is passed on | 196 /// Configures [future] so that its result (success or exception) is passed on |
197 /// to [completer]. | 197 /// to [completer]. |
198 void chainToCompleter(Future future, Completer completer) { | 198 void chainToCompleter(Future future, Completer completer) { |
199 future.then(completer.complete, onError: completer.completeError); | 199 future.then(completer.complete, onError: completer.completeError); |
200 } | 200 } |
201 | 201 |
202 // TOOD(nweiz): Get rid of this once https://codereview.chromium.org/11293132/ | |
203 // is in. | |
204 /// Runs [fn] for each element in [input] in order, moving to the next element | |
205 /// only when the [Future] returned by [fn] completes. Returns a [Future] that | |
206 /// completes when all elements have been processed. | |
207 /// | |
208 /// The return values of all [Future]s are discarded. Any errors will cause the | |
209 /// iteration to stop and will be piped through the return value. | |
210 Future forEachFuture(Iterable input, Future fn(element)) { | |
211 var iterator = input.iterator; | |
212 Future nextElement(_) { | |
213 if (!iterator.moveNext()) return new Future.value(); | |
214 return fn(iterator.current).then(nextElement); | |
215 } | |
216 return nextElement(null); | |
217 } | |
218 | |
219 /// Like [Future.sync], but wraps the Future in [Chain.track] as well. | 202 /// Like [Future.sync], but wraps the Future in [Chain.track] as well. |
220 Future syncFuture(callback()) => Chain.track(new Future.sync(callback)); | 203 Future syncFuture(callback()) => Chain.track(new Future.sync(callback)); |
OLD | NEW |