@louis I’m glad we agree async/await, Promise
s, and Fiber
s are important and exciting I’m not sure exactly what you’ve tried already, but maybe I can say some things that will help you get unstuck.
Here’s an unreleased package I built that polyfills Promise
on both client and server (though of course Fibers only work on the server). That commit is part of an experimental branch that replaces all uses of Future
with Promise
in the code for the command-line tool.
Using that package (or something like it) should give you a reliable Promise
constructor that you can use without any special compilation steps, including Promise.async
and Promise.await
and a few other Meteor-specific methods.
If you want to use actual ES7 async
and await
syntax, transpiled to Promise.async
and Promise.await
, that’s a little trickier, but basically it’s a matter of creating a package that registers a source handler using Plugin.registerSourceHandler
, similar to the jsx plugin that @sashko mentioned. I’m planning to release a package for that soon, and I’ll be sure to comment on this thread about my progress.
The package I have in mind will use the meteor-babel
NPM package, which includes a special transform that compiles async
and await
syntax to Promise.asyncApply
and Promise.await
. For that to work, you have to be using the Promise
constructor provided by meteor-promise
, and you have to enable async
and await
using a special option:
var meteorBabel = require("meteor-babel");
var babelOptions = meteorBabel.getDefaultOptions({
// This option tweaks the default options to enable parsing async/await
// and also apply the meteor-async-await transform.
meteorAsyncAwait: true
});
// Modify babelOptions however you like here, before passing them to
// meteorBabel.compile.
var result = meteorBabel.compile(source, babelOptions);
var transpiledCode = result.code;
One small note, since you’ve read the slides: the Babel parser currently does not allow await
expressions to appear outside async
function bodies, but you can use Promise.await(argument)
instead of await argument
anywhere you like, as long as your code is running in a Fiber
.