Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ES 7 async/await demo!
babel github-es6.js -o github.js --optional runtime --experimental
import request from "request";
// promise returning function
function get (url){
return new Promise(function(resolve, reject){
request({
method: 'GET',
url: url,
json: true,
headers: {
'User-Agent': 'request'
}
}, function(err, resp, body){
if(err){
reject(err);
} else {
resolve(body);
}
});
});
}
// create a new "async" function so we can use the "await" keyword
async function printPublicGists(){
// "await" resolution or rejection of the promise
// use try/catch for error handling
try {
var gists = await get('https://api.github.com/gists/public');
// now you can write this like syncronous code!
gists.forEach(function(gist){
console.log(gist.description);
});
} catch (e) {
// promise was rejected and we can handle errors with try/catch!
}
}
printPublicGists();
"use strict";
var _core = require("babel-runtime/core-js")["default"];
var _regeneratorRuntime = require("babel-runtime/regenerator")["default"];
var _babelHelpers = require("babel-runtime/helpers")["default"];
var request = _babelHelpers.interopRequire(require("request"));
// promise returning function
function get(url) {
return new _core.Promise(function (resolve, reject) {
request({
method: "GET",
url: url,
json: true,
headers: {
"User-Agent": "request"
}
}, function (err, resp, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
});
});
}
// create a new "async" function so we can use the "await" keywork
function printPublicGists() {
var gists;
return _regeneratorRuntime.async(function printPublicGists$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
context$1$0.next = 3;
return get("https://api.github.com/gists/public");
case 3:
gists = context$1$0.sent;
// now you can write this like syncronous code!
gists.forEach(function (gist) {
console.log(gist.description);
});
context$1$0.next = 9;
break;
case 7:
context$1$0.prev = 7;
context$1$0.t0 = context$1$0["catch"](0);
case 9:
case "end":
return context$1$0.stop();
}
}, null, this, [[0, 7]]);
}
printPublicGists();
// "await" resolution or rejection of the promise
// use try/catch for error handling
// promise was rejected and we can handle errors with try/catch!
@meteormatt

This comment has been minimized.

Copy link

commented Jun 16, 2016

I run build.sh, but get this error

SyntaxError: github-es6.js: Unexpected token (24:6)
  22 | 
  23 | // create a new "async" function so we can use the "await" keyword
> 24 | async function printPublicGists(){
     |       ^
  25 |   // "await" resolution or rejection of the promise
  26 |   // use try/catch for error handling
  27 |   try {
@piecioshka

This comment has been minimized.

Copy link

commented Sep 5, 2016

@meteormatt what version of Babel you have installed?

@d668

This comment has been minimized.

Copy link

commented Sep 22, 2016

I am getting
Uncaught (in promise) TypeError: Class constructor HTTP cannot be invoked without 'new'(…)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.