Last active
November 29, 2017 13:24
-
-
Save jthegedus/56f18e67899c99695a04473a3b5a101c to your computer and use it in GitHub Desktop.
ES6+ in Cloud Functions for Firebase - preset-env to minimally compile async-await
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Babel output - with Babel REPL settings: | |
// * prettify | |
// * preset-env Node v6.9 | |
"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"); | |
}); | |
}; | |
} | |
const functions = require("firebase-functions"); | |
// returns a string after 5 seconds | |
const message = () => { | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(`from Babelified Cloud Functions!`); | |
}, 5000); | |
}); | |
}; | |
module.exports.helloWorld = functions.https.onRequest( | |
(() => { | |
var _ref = _asyncToGenerator(function*(req, res) { | |
let world = yield message(); | |
res.status(200).send(`Hello ${world}`); | |
}); | |
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