admin管理员组文章数量:1295947
I'm creating an AWS SAM application using Node.js Lambda functions. The default template has an async handler function:
exports.lambdaHandler = async (event, context) => {
// ...
return {
statusCode: 200,
body: JSON.stringify({ hello: "world" })
};
};
Is there any benefit to having this handler function be async
vs sync
, since my understanding is each time a Lambda function is invoked it runs separately from other instances?
I'm creating an AWS SAM application using Node.js Lambda functions. The default template has an async handler function:
exports.lambdaHandler = async (event, context) => {
// ...
return {
statusCode: 200,
body: JSON.stringify({ hello: "world" })
};
};
Is there any benefit to having this handler function be async
vs sync
, since my understanding is each time a Lambda function is invoked it runs separately from other instances?
-
3
async
allows you to useawait
in the function body. If you're not usingawait
, then there is very little reason to useasync
. – VLAZ Commented May 10, 2021 at 16:26
2 Answers
Reset to default 7AWS Lambda handles synchronous functions and asynchronous functions as well. async
means two things:
- The function returns a
Promise
- You are able to use
await
inside it
AWS Lambda happens to understand Promises as return value, thats why async
functions work as well. So if you need await
just go for async
.
You could also not declare the function as async
and return
a Promise
(or chain of Promises)
use async handler function if you want to use return and throw to send a response or error respectively from your function handler.
you can use non-async handler but it only terminates the function execution when the event loop is empty or the function times out.
ref: https://docs.aws.amazon./lambda/latest/dg/nodejs-handler.html
本文标签:
版权声明:本文标题:javascript - AWS Lambda: Is there a benefit to using an async handler function for the Node runtime? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741625853a2389076.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论