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)); |
nweiz
2013/01/09 02:24:50
Nit: need a space between "=>" and "completer".
Bob Nystrom
2013/01/09 02:26:41
Oopsie. Done.
| |
125 .catchError((e) { | 125 |
nweiz
2013/01/09 02:24:50
Extra line
Bob Nystrom
2013/01/09 02:26:41
Done.
| |
126 completer.completeError(e.error, e.stackTrace); | |
127 }); | |
128 } | 126 } |
129 | 127 |
130 // TODO(nweiz): unify the following functions with the utility functions in | 128 // TODO(nweiz): unify the following functions with the utility functions in |
131 // pkg/http. | 129 // pkg/http. |
132 | 130 |
133 /// Like [String.split], but only splits on the first occurrence of the pattern. | 131 /// 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. | 132 /// This will always return an array of two elements or fewer. |
135 List<String> split1(String toSplit, String pattern) { | 133 List<String> split1(String toSplit, String pattern) { |
136 if (toSplit.isEmpty) return <String>[]; | 134 if (toSplit.isEmpty) return <String>[]; |
137 | 135 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 // TODO(rnystrom): Remove this when #7781 is fixed. | 188 // TODO(rnystrom): Remove this when #7781 is fixed. |
191 /// When an error is rethrown in an async callback, you can end up with nested | 189 /// 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. | 190 /// AsyncErrors. This unwraps them to find the real originating error. |
193 getRealError(error) { | 191 getRealError(error) { |
194 while (error is AsyncError) { | 192 while (error is AsyncError) { |
195 error = error.error; | 193 error = error.error; |
196 } | 194 } |
197 | 195 |
198 return error; | 196 return error; |
199 } | 197 } |
OLD | NEW |