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 /// Generic utility functions. Stuff that should possibly be in core. | 5 /// Generic utility functions. Stuff that should possibly be in core. |
6 library utils; | 6 library utils; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:crypto'; | 9 import 'dart:crypto'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 /// Returns a [Future] that completes in [milliseconds]. | 113 /// Returns a [Future] that completes in [milliseconds]. |
114 Future sleep(int milliseconds) { | 114 Future sleep(int milliseconds) { |
115 var completer = new Completer(); | 115 var completer = new Completer(); |
116 new Timer(milliseconds, (_) => completer.complete()); | 116 new Timer(milliseconds, (_) => completer.complete()); |
117 return completer.future; | 117 return completer.future; |
118 } | 118 } |
119 | 119 |
120 /// Configures [future] so that its result (success or exception) is passed on | 120 /// Configures [future] so that its result (success or exception) is passed on |
121 /// to [completer]. | 121 /// to [completer]. |
122 void chainToCompleter(Future future, Completer completer) { | 122 void chainToCompleter(Future future, Completer completer) { |
123 future | 123 future.then((value) => completer.complete(value), |
124 .then(completer.complete) | 124 onError: (e) => completer.completeError(e.error, e.stackTrace)); |
125 .catchError((e) { | |
126 completer.completeError(e.error, e.stackTrace); | |
127 }); | |
128 } | 125 } |
129 | 126 |
130 // TODO(nweiz): unify the following functions with the utility functions in | 127 // TODO(nweiz): unify the following functions with the utility functions in |
131 // pkg/http. | 128 // pkg/http. |
132 | 129 |
133 /// Like [String.split], but only splits on the first occurrence of the pattern. | 130 /// Like [String.split], but only splits on the first occurrence of the pattern. |
134 /// This will always return an array of two elements or fewer. | 131 /// This will always return an array of two elements or fewer. |
135 List<String> split1(String toSplit, String pattern) { | 132 List<String> split1(String toSplit, String pattern) { |
136 if (toSplit.isEmpty) return <String>[]; | 133 if (toSplit.isEmpty) return <String>[]; |
137 | 134 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // TODO(rnystrom): Remove this when #7781 is fixed. | 187 // TODO(rnystrom): Remove this when #7781 is fixed. |
191 /// When an error is rethrown in an async callback, you can end up with nested | 188 /// When an error is rethrown in an async callback, you can end up with nested |
192 /// AsyncErrors. This unwraps them to find the real originating error. | 189 /// AsyncErrors. This unwraps them to find the real originating error. |
193 getRealError(error) { | 190 getRealError(error) { |
194 while (error is AsyncError) { | 191 while (error is AsyncError) { |
195 error = error.error; | 192 error = error.error; |
196 } | 193 } |
197 | 194 |
198 return error; | 195 return error; |
199 } | 196 } |
OLD | NEW |