OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// This library adapts ES6 generators to implement Dart's async/await. | 5 /// This library adapts ES6 generators to implement Dart's async/await. |
6 /// It's designed to interact with Dart's Future/Stream and follow Dart | 6 /// It's designed to interact with Dart's Future/Stream and follow Dart |
7 /// async/await semantics. | 7 /// async/await semantics. |
8 /// See https://github.com/dart-lang/sdk/issues/27315 for ideas on | 8 /// See https://github.com/dart-lang/sdk/issues/27315 for ideas on |
9 /// reconciling Dart's Future and ES6 Promise. | 9 /// reconciling Dart's Future and ES6 Promise. |
10 /// Inspired by `co`: https://github.com/tj/co/blob/master/index.js, which is a | 10 /// Inspired by `co`: https://github.com/tj/co/blob/master/index.js, which is a |
(...skipping 24 matching lines...) Expand all Loading... | |
35 // | 35 // |
36 // In essence, we are giving the code inside the generator a chance to | 36 // In essence, we are giving the code inside the generator a chance to |
37 // use try-catch-finally. | 37 // use try-catch-finally. |
38 return next(iter.throw(err)); | 38 return next(iter.throw(err)); |
39 } | 39 } |
40 function next(ret) { | 40 function next(ret) { |
41 if (ret.done) return ret.value; | 41 if (ret.done) return ret.value; |
42 // Checks if the awaited value is a Future. | 42 // Checks if the awaited value is a Future. |
43 let future = ret.value; | 43 let future = ret.value; |
44 if (!$instanceOf(future, ${getGenericClass(Future)})) { | 44 if (!$instanceOf(future, ${getGenericClass(Future)})) { |
45 future = $Future.value(future); | 45 var result = future; |
Jennifer Messerly
2017/01/25 18:31:17
as noted, this change appears to contradict the sp
| |
46 future = $Future.microtask(() => result); | |
46 } | 47 } |
47 // Chain the Future so `await` receives the Future's value. | 48 // Chain the Future so `await` receives the Future's value. |
48 return future.then($dynamic)(onValue, {onError: onError}); | 49 return future.then($dynamic)(onValue, {onError: onError}); |
49 } | 50 } |
50 return ${getGenericClass(Future)}($T).new(function() { | 51 return ${getGenericClass(Future)}($T).microtask(function() { |
Jennifer Messerly
2017/01/25 18:31:17
here's the spec for the return value, 16.14 Functi
| |
51 iter = $gen.apply(null, $args)[Symbol.iterator](); | 52 iter = $gen.apply(null, $args)[Symbol.iterator](); |
52 return onValue(); | 53 return onValue(); |
53 }); | 54 }); |
54 })()'''); | 55 })()'''); |
55 | 56 |
56 // Implementation inspired by _AsyncStarStreamController in | 57 // Implementation inspired by _AsyncStarStreamController in |
57 // dart-lang/sdk's runtime/lib/core_patch.dart | 58 // dart-lang/sdk's runtime/lib/core_patch.dart |
58 // | 59 // |
59 // Given input like: | 60 // Given input like: |
60 // | 61 // |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 this.controller.addError(error, stackTrace); | 216 this.controller.addError(error, stackTrace); |
216 } | 217 } |
217 } | 218 } |
218 '''); | 219 '''); |
219 | 220 |
220 /// Returns a Stream of T implemented by an async* function. */ | 221 /// Returns a Stream of T implemented by an async* function. */ |
221 /// | 222 /// |
222 asyncStar(gen, T, @rest args) => JS('', '''(() => { | 223 asyncStar(gen, T, @rest args) => JS('', '''(() => { |
223 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; | 224 return new $_AsyncStarStreamController($gen, $T, $args).controller.stream; |
224 })()'''); | 225 })()'''); |
OLD | NEW |