Reputation: 367
I've been creating lambda@edge functions to do various actions on a viewer request
event from cloudfront.
Most examples I can find seem to use callbacks but I wanted to use the async/await pattern instead, so I changed the handler function from:
exports.handler = (event, context, callback) => {
to:
exports.handler = async (event, context) => {
Examples using callbacks are returning errors via the first parameter in the callback such as:
callback(new Error("invalid token"));
My question is, with the async way is this code exactly equivalent?
throw new Error("invalid token")
As an aside, I'm not sure returning an error is the best approach anyway, and returning a response object with correct http code might be better.
Upvotes: 0
Views: 104
Reputation: 4946
Yes, from the documentation, async functions always return a promise so throw new Error("invalid token")
exception would be wrapped into a promise and returned, similar to callback
returning error.
Alternatively, you can use Promise.reject() to manually wrap the error into a promise
return Promise.reject(new Error("invalid token"));
Upvotes: 0