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 deprecatedFutureValue(_FutureImpl future) => | 7 deprecatedFutureValue(_FutureImpl future) => |
8 future._isComplete ? future._resultOrListeners : null; | 8 future._isComplete ? future._resultOrListeners : null; |
9 | 9 |
10 | 10 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 return new Future<List>.immediate(const []); | 102 return new Future<List>.immediate(const []); |
103 } | 103 } |
104 | 104 |
105 Completer completer = new Completer<List>(); | 105 Completer completer = new Completer<List>(); |
106 int remaining = futures.length; | 106 int remaining = futures.length; |
107 List values = new List.fixedLength(futures.length); | 107 List values = new List.fixedLength(futures.length); |
108 | 108 |
109 // As each future completes, put its value into the corresponding | 109 // As each future completes, put its value into the corresponding |
110 // position in the list of values. | 110 // position in the list of values. |
111 int i = 0; | 111 int i = 0; |
| 112 bool completed = false; |
112 for (Future future in futures) { | 113 for (Future future in futures) { |
113 int pos = i++; | 114 int pos = i++; |
114 future.then((Object value) { | 115 future.then((Object value) { |
115 values[pos] = value; | 116 values[pos] = value; |
116 if (--remaining == 0) { | 117 if (--remaining == 0) { |
117 completer.complete(values); | 118 completer.complete(values); |
118 } | 119 } |
119 }); | 120 }).catchError((error) { |
120 future.catchError((error) { | 121 if (!completed) completer.completeError(error.error, error.stackTrace); |
121 completer.completeError(error.error, error.stackTrace); | 122 completed = true; |
122 }); | 123 }); |
123 } | 124 } |
124 | 125 |
125 return completer.future; | 126 return completer.future; |
126 } | 127 } |
127 | 128 |
128 Future then(f(T value), { onError(AsyncError error) }) { | 129 Future then(f(T value), { onError(AsyncError error) }) { |
129 if (!_isComplete) { | 130 if (!_isComplete) { |
130 if (onError == null) { | 131 if (onError == null) { |
131 return new _ThenFuture(f).._subscribeTo(this); | 132 return new _ThenFuture(f).._subscribeTo(this); |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 Future catchError(function(AsyncError error), {bool test(var error)}) { | 454 Future catchError(function(AsyncError error), {bool test(var error)}) { |
454 return _future.catchError(function, test: test); | 455 return _future.catchError(function, test: test); |
455 } | 456 } |
456 | 457 |
457 Future whenComplete(void action()) { | 458 Future whenComplete(void action()) { |
458 return _future.whenComplete(action); | 459 return _future.whenComplete(action); |
459 } | 460 } |
460 | 461 |
461 Stream<T> asStream() => new Stream.fromFuture(this); | 462 Stream<T> asStream() => new Stream.fromFuture(this); |
462 } | 463 } |
OLD | NEW |