In AWS lambda, before node.js runtime 8.10, although you can use promise across all your code, but in top handler, you must follow the below callback
pattern. async/await is not supported in handler, even in other functions you must use something like babel to transform your code before deploy.
Previous callback pattern
1 | const api = require('something'); |
With the new Node.js 8.10, you can use promise or async/await pattern in handler, just like normal node.js function. And you don’t need to pass context and callback parameters, only event.
Use async/await
for sync value without await:
1 | exports.handler = async (event) => { |
for async value with await:
1 | const api = require('something'); |
Use Promise
for sync value wrap in Promise:
1 | exports.handler = (event) => { |
for async:
you can directly return it, but I don’t recommend this way
1 | const api = require('something'); |
It’s better start from Promise.resolve():
1 | exports.handler = (event) => { |
Summary
To use async or Promise, it’s up to your choice. But I’m sure either way is easier than the previous callback pattern. Also to mention again, async/await is actually a syntactic sugar for promise in js.
Ref:
https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/