I don’t see a lot of people pushing for async/await style programming, so I figured I’d bring it to everyone’s attention now that Meteor 1.3 (beta) has built-in support for it.
The biggest use-case for async/await is getting rid of “callback hell” in an even more efficient way than using Promises. Take the following example:
Meteor.call('myFunction', function(error, result) {
if(error) {
throw error;
}
Meteor.call('myFunction2', result, function(error, result2) {
if(error) {
throw error;
}
// Do stuff
});
});
With the help of async/await and the deanius:promise package, we can accomplish the same thing like so:
(async function() {
var result = await Meteor.callPromise('myFunction', <arg1>, <arg2>);
var result2 = await Meteor.callPromise('myFunction2', result, <arg2>, <arg3>);
// Do stuff
}());
Meteor.call() only supports callbacks, so we use the deanius:promise package that adds the Meteor.callPromise() method which, you guessed it, does the exact same thing as Meteor.call() except it returns a promise.
Async/await has really streamlined my code, and I highly recommend people adopt this pattern. For more information, check out the following links:
A good write-up about async/await.
An excellent video by one of the guys working on the ES7 spec. The link should skip you ahead to about 10:26 where he starts talking about async programming. He details exactly what async/await is doing behind the scenes. It’s essentially syntactic sugar for a complex generator pattern. Incidentally, this generator pattern is what Babel uses to transpile the async/await keywords. If you have time, you should watch the entire thing from the beginning.