-
Notifications
You must be signed in to change notification settings - Fork 27
Implement async / await (and Stream variants) #221
Comments
I would recommend a native approach, not the async/await transformer. The transformer goes through a lot of effort to (1) make synchronous code fully synchronous, (2) use constant space for synchronous looping via a trampoline, and (3) avoid a space leak in the dart Future implementation when awaiting in a loop. There is also a lot of duplicated exception handling code (e.g., around every callback body). All of that should be avoidable with a native encoding as a generator of futures, such as the one you sketch. |
Here's a complete example that runs on io.js.
var f = (g) => dart.consumeAwaits((function*() {
var callback$1;
try {
var value = ((yield new dart.Await(g(), cb => callback$1 = cb)) || callback$1());
return 'Result: ' + (value + 1);
} catch (e) {
if (e == 'rethrow me') throw e;
return 'Caught: ' + e;
}
})()); |
DDC doesn't type check async either: #151 There's another option for implementation: dart2js has a transform that operates on their JS trees (which DDC is using an older version of + ES6 nodes in ours). https://github.com/dart-lang/sdk/blob/master/pkg/compiler/lib/src/js/rewrite_async.dart ... That would be more relevant to generating ES5 though. But yeah, for ES6, expressing it on top of generators is almost certainly the way to go. Anything that avoids building the state machine is going to be more readable :). There's a lot of subtleties to think through though. |
taking a look at this soon. More stuff is depending on it (including package:test now) so we aren't going to get very far without it. |
awesome, thanks Vijay! |
almost ready to send initial |
https://codereview.chromium.org/1207313002/ |
I'm tempted to use the co library: https://github.com/tj/co, which adapts JS generators to Promises. But we'd need to solve #245 first. edit: |
Right now it maps straight to ES6 generators. This should handle some basic cases, but there's more work to get all of the corners working. For example: it doesn't implement Iterable yet. R=vsm@google.com Review URL: https://codereview.chromium.org/1207313002.
Looks like this should be relatively easy with ES6 generators:
Could be transpiled to something like:
With a helper along these lines:
Another option would be to dig the defunct async/await pub transformer from the grave, but it might produce less ES6-idiomatic code?
The text was updated successfully, but these errors were encountered: