I use better-babel-cli to launch the babel and transfer parameters to the plug-ins from the console (although there is no need for this, it’s just too lazy to do babelrc).
you can compile like this:
babel --async-to-generator --transform-regenerator -- async.js > babel_async.js
async.js:
var bar = function(){ return new Promise( function( good, bad ){ setTimeout( good, 1000, ' good' ); } ); } async function foo() { console.log( 'checking async life' ); var res = await bar(); console.log( 'async life is ' + res ) } foo();
Result:
let foo = (() => { var ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { var res; return regeneratorRuntime.wrap(function _callee$(_context) { while (1){ switch (_context.prev = _context.next) { case 0: console.log('checking async life'); _context.next = 3; return bar(); case 3: res = _context.sent; console.log('async life is ' + res); case 5: case 'end': return _context.stop(); } } }, _callee, this); })); return function foo() { return ref.apply(this, arguments); }; })(); function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; } var bar = function () { return new Promise(function (good, bad) { console.log('new promise'); setTimeout(good, 1000, ' good'); });
};
--async-to-generator plugin to convert async / await to es6 generators
--transform-generator - a plugin for converting es6 generators (part es2015) to es5 (for the work of es5 the transformed code will need to load an additional library), you can take it here )
If you do not need to run in ie, you can do it without the second plug-in.
Change:
Added link to async plugin.
By the way, async / await is not es6 code, not even es7 (await is mentioned only as a reserved word).