| 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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 if (other is! Pair) return false; | 209 if (other is! Pair) return false; |
| 210 return other.first == first && other.last == last; | 210 return other.first == first && other.last == last; |
| 211 } | 211 } |
| 212 | 212 |
| 213 int get hashCode => first.hashCode ^ last.hashCode; | 213 int get hashCode => first.hashCode ^ last.hashCode; |
| 214 } | 214 } |
| 215 | 215 |
| 216 /// Configures [future] so that its result (success or exception) is passed on | 216 /// Configures [future] so that its result (success or exception) is passed on |
| 217 /// to [completer]. | 217 /// to [completer]. |
| 218 void chainToCompleter(Future future, Completer completer) { | 218 void chainToCompleter(Future future, Completer completer) { |
| 219 future.then((v) => completer.complete(v)).catchError((error) { | 219 future.then(completer.complete, onError: completer.completeError); |
| 220 completer.completeError(error); | |
| 221 }); | |
| 222 } | 220 } |
| 223 | 221 |
| 224 // TOOD(nweiz): Get rid of this once https://codereview.chromium.org/11293132/ | 222 // TOOD(nweiz): Get rid of this once https://codereview.chromium.org/11293132/ |
| 225 // is in. | 223 // is in. |
| 226 /// Runs [fn] for each element in [input] in order, moving to the next element | 224 /// Runs [fn] for each element in [input] in order, moving to the next element |
| 227 /// only when the [Future] returned by [fn] completes. Returns a [Future] that | 225 /// only when the [Future] returned by [fn] completes. Returns a [Future] that |
| 228 /// completes when all elements have been processed. | 226 /// completes when all elements have been processed. |
| 229 /// | 227 /// |
| 230 /// The return values of all [Future]s are discarded. Any errors will cause the | 228 /// The return values of all [Future]s are discarded. Any errors will cause the |
| 231 /// iteration to stop and will be piped through the return value. | 229 /// iteration to stop and will be piped through the return value. |
| 232 Future forEachFuture(Iterable input, Future fn(element)) { | 230 Future forEachFuture(Iterable input, Future fn(element)) { |
| 233 var iterator = input.iterator; | 231 var iterator = input.iterator; |
| 234 Future nextElement(_) { | 232 Future nextElement(_) { |
| 235 if (!iterator.moveNext()) return new Future.value(); | 233 if (!iterator.moveNext()) return new Future.value(); |
| 236 return fn(iterator.current).then(nextElement); | 234 return fn(iterator.current).then(nextElement); |
| 237 } | 235 } |
| 238 return nextElement(null); | 236 return nextElement(null); |
| 239 } | 237 } |
| OLD | NEW |