There are a lot of fun and useful resources on what you can use a Lambda Function for. This post just goes over the basic structure of a Netlify Function. The recommended way is to use the async
keyword instead of a callback
function when writing Netlify Functions in JavaScript. For example:
exports.handler = async function(event, context) {
console.log("EVENT: \n" + JSON.stringify(event, null, 2))
return context.logStreamName
}
For posterity, the following non-async
method using callback
is still supported, but the async
syntax is more versatile. An example of a callback
function is below:
exports.handler = function(event, context, callback) {
https.get(url, (res) => {
callback(null, res.statusCode)
}).on('error', (e) => {
callback(Error(e))
})
}
For more information regarding best practices, check out the Netlify docs on Functions.