Skip to content

Instantly share code, notes, and snippets.

@bozzmob
Created June 15, 2016 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bozzmob/2d0108aa447d2bf2da2fb19f6a2c5cb4 to your computer and use it in GitHub Desktop.
Save bozzmob/2d0108aa447d2bf2da2fb19f6a2c5cb4 to your computer and use it in GitHub Desktop.
require("babel-core/register");
require("babel-polyfill");
(async function testingAsyncAwait() {
await console.log("For Trump's Sake Print me!");
})();
@bozzmob
Copy link
Author

bozzmob commented Jun 15, 2016

What am I missing? Same thing works finely on my Linux(not sure what else I've done there ages back), but, it doesn't work on MAC right now.

Why does it say 'Unexpected token' for either async or function keywords?

BOZZMOB-M-T0HZ:rest bozzmob$ node -v
v6.2.1


BOZZMOB-M-T0HZ:rest bozzmob$ babel helloz.js > helloz.trans.js
SyntaxError: helloz.js: Unexpected token (4:7)
  2 | require("babel-polyfill");
  3 | 
> 4 | (async function testingAsyncAwait() {
    |        ^
  5 |     await console.log("For Trump's Sake Print me!");
  6 | })();


BOZZMOB-M-T0HZ:rest bozzmob$ node helloz.js 
/Users/bozzmob/Documents/work/rest/helloz.js:4
(async function testingAsyncAwait() {
       ^^^^^^^^
SyntaxError: Unexpected token function
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:160:18)
    at node.js:456:3

@bharathitman
Copy link

bharathitman commented Jun 15, 2016

Does babel support async await?. It's ES 2017 no?

@peterlazar1993
Copy link

Hey Satvik, for async await you need to have transform-regenerator and syntax-async-functions plugin installed and specified in .babelrc

@bozzmob
Copy link
Author

bozzmob commented Jun 15, 2016

@bharathitman No. True, I was wrong. had to use- Babel preset es2017 plugin

And then did this-

BOZZMOB-M-T0HZ:rest bozzmob$ babel helloz.js --preset > es2017.js

es2017.js-

"use strict";

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"); }); }; }

require("babel-core/register");
require("babel-polyfill");

(() => {
    var ref = _asyncToGenerator(function* () {
        yield console.log("For Trump's Sake Print me!");
    });

    function testingAsyncAwait() {
        return ref.apply(this, arguments);
    }

    return testingAsyncAwait;
})()();

@bozzmob
Copy link
Author

bozzmob commented Jun 15, 2016

@peterlazar1993 Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment