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