Context vs Callback in AWS Lambda

后端 未结 2 1608
暗喜
暗喜 2021-01-11 15:52

I am loving using lambda functions in AWS. It ideally reduced my time in maintaining the servers anymore. My question is when using lambda there is context object and the ca

相关标签:
2条回答
  • 2021-01-11 16:24

    Here callback represented as improvement for context.succeed.

    In addition, the event loop allows Node.js to perform non-blocking I/O operations. if callback waits longer then you expect, it means there are other not-completed tasks in the event loop you are not aware of and this is a problem - you should know what tasks are waiting in the queue. You can use the following functions to understand why callback is waiting:

    process._getActiveHandles() //gets you handles that are still alive
    process._getActiveRequests() //gets you info about active libuv requests.
    

    Freezing the lambda before completing those tasks may lead to unexpected behavior. The next lambda execution can be done on the same container. It means event loop tasks of the previous lambda execution are completed in the current lambda execution.

    Also, assuming you have more than one test for exports.handler, with callback every test is independent. With context.succeed, you second test can pass (instead of failing) because of the first test, leaving tasks in the event loop.

    0 讨论(0)
  • 2021-01-11 16:28

    context.succeed is the older way of doing things, and is supported under the 0.10.42 runtime (where the callback parameter specifically isn't). If you're running on the newer runtimes (4.3 and 6.10), it's included for backwards compatibility, but the "proper" way is now to use the callback functionality.

    0 讨论(0)
提交回复
热议问题