Skip to content

Instantly share code, notes, and snippets.

@jthegedus
Last active November 29, 2017 13:24
Show Gist options
  • Save jthegedus/bc47cb25f4d032355073c4285b92c599 to your computer and use it in GitHub Desktop.
Save jthegedus/bc47cb25f4d032355073c4285b92c599 to your computer and use it in GitHub Desktop.
ES6+ in Cloud Functions for Firebase - preset-2015,2016,2017 to compile async/await
// Babel output - with Babel REPL settings:
// * prettify
// * preset-es2015 (es6->es5)
// * preset-es2016 (es7->es6)
// * preset-es2017 (es8->es7)
"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) {
step("next", value);
},
function(err) {
step("throw", err);
}
);
}
}
return step("next");
});
};
}
var functions = require("firebase-functions");
// returns a string after 5 seconds
var message = function message() {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("from Babelified Cloud Functions!");
}, 5000);
});
};
module.exports.helloWorld = functions.https.onRequest(
(function() {
var _ref = _asyncToGenerator(
/*#__PURE__*/ regeneratorRuntime.mark(function _callee(req, res) {
var world;
return regeneratorRuntime.wrap(
function _callee$(_context) {
while (1) {
switch ((_context.prev = _context.next)) {
case 0:
_context.next = 2;
return message();
case 2:
world = _context.sent;
res.status(200).send("Hello " + world);
case 4:
case "end":
return _context.stop();
}
}
},
_callee,
undefined
);
})
);
return function(_x, _x2) {
return _ref.apply(this, arguments);
};
})()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment