javascript AWS Lambda 中的上下文与回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47906116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:41:16  来源:igfitidea点击:

Context vs Callback in AWS Lambda

javascriptnode.jsamazon-web-servicesaws-lambda

提问by Naveen Kerati

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 callback function to terminate the function. Is there any use case of using callback over context.

我喜欢在 AWS 中使用 lambda 函数。它理想地减少了我维护服务器的时间。我的问题是使用 lambda 时有上下文对象和回调函数来终止函数。是否有在上下文中使用回调的用例。

Could anyone tell me the behavior of context.succeed() to callback(error,message)

谁能告诉我 context.succeed() 到 callback(error,message) 的行为

var startedAt = new Date();

var interval = setInterval(function () {
    console.log(startedAt, new Date());
}, 1000);

exports.handler = function (event, context, callback) {
    setTimeout(function () {
        console.log('returning');
        // v1:
        return callback(null);
        // v2:
        // return context.succeed();
    }, 5000);
};

回答by James Thorpe

context.succeedis the older way of doing things, and is supported under the 0.10.42 runtime (where the callbackparameter 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 callbackfunctionality.

context.succeed是较旧的做事方式,并且在 0.10.42 运行时(callback特别是参数不是)下受支持。如果您在较新的运行时(4.3 和 6.10)上运行,则包含它是为了向后兼容,但现在“正确”的方法是使用该callback功能。

回答by KernelMode

Herecallbackrepresented as improvement for context.succeed.

这里callback表示为 的改进context.succeed

In addition, the event loopallows Node.js to perform non-blocking I/O operations. if callbackwaits 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 functionsto understand why callbackis waiting:

此外,事件循环允许 Node.js 执行非阻塞 I/O 操作。如果callback等待的时间比您预期的要长,这意味着您不知道事件循环中还有其他未完成的任务,这是一个问题 - 您应该知道队列中正在等待哪些任务。您可以使用以下函数来了解callback等待的原因:

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.

在完成这些任务之前冻结 lambda 可能会导致意外行为。下一个 lambda 执行可以在同一个容器上完成。这意味着前一次 lambda 执行的事件循环任务在当前 lambda 执行中完成。

Also, assuming you have more than one test for exports.handler, with callbackevery 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.

此外,假设您对 进行了多个测试exports.handlercallback每个测试都是独立的。使用context.succeed,由于第一个测试,您的第二个测试可以通过(而不是失败),从而将任务留在事件循环中。