从Node.js中的AWS Lambda函数返回的正确方法是什么? [英] What's the right way to return from an AWS Lambda function in Node.js?

查看:41
本文介绍了从Node.js中的AWS Lambda函数返回的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是AWS Lambda的新手,有一件事我感到非常困惑.

I am new to AWS Lambda and there is one thing I find very confusing.

到目前为止,我发现以下选项如何从Node.js中的函数返回:

So far, I found following options how to return from a function in Node.js:

1.

exports.handler = (event, context) => {
    context.succeed('ok');
}

2.

exports.handler = (event, context) => {
    context.done(null, 'ok');
}

3.

exports.handler = (event, context, callback) => {
    callback(null, 'ok');
}

4.

exports.handler = async event => {
    return "ok";
}

这些有何不同?有功能或性能上的区别吗?

How are these different? Any functionality or performance distinctions?

任何人都可以解释如何以正确的方式终止功能吗?

Can anyone explain how to terminate a function in the right way?

推荐答案

您可能正在使用Node.js 8.10,这是迄今为止AWS Lambda支持的最新Node.js版本,否则是最后一个(4.)代码段.根本不起作用(由于语法错误).

You're probably using Node.js 8.10, which is so far the last Node.js version supported by AWS Lambda, otherwise the last (4.) snippet woudn't work at all (due to a syntax error).

在Node.js 8.10中,以上列出的所有变体都是有效的,但大多数变体仍然存在,仅是为了与早期运行时版本兼容.

In Node.js 8.10 all the above listed variants are valid, most of them are still there only for compatibility with earlier runtime versions.

第二个拳头(第一个和第一个)是最老的两个,因此不建议再使用它们. done(err ?, res?)函数等效于后来添加的 callback(err ?, res?),后者在Node.js 8.10和更高版本中经常使用.即使在官方文档中,您仍然可以找到很多代码示例.这是一个典型的回调,可用于异步处理:

The fist two (1. and 2.) are the oldest ones and it's not recomended to use them anymore. The done(err?, res?) function is an equivalent to the later added callback(err?, res?), which was frequently used before Node.js 8.10 and you can still find a lot of code examples even in the official documentation. It's a typical callback and could be used in asynchronous processing:

exports.handler = (event, context, callback) => {
    var params = {
        Bucket: "examplebucket", 
        Key: "HappyFace.jpg"
    };
    s3.getObject(params, function(err, data) {
        if (err) return callback(err);
        callback(null, data);
    });    
}

尽管如此,此函数具有一般使用回调(回调地狱)的所有缺点.

Nevertheless, this function has all the drawbacks of using callbacks in general (Callback Hell).

在Node.js 8.10上,您可以将Promises与 async/await 语法糖一起使用,这使异步计算看起来像是同步的.AWS JavaScript SDK向以前使用回调的几乎所有函数中添加了一个函数 promise().现在您可以编写:

Up Node.js 8.10 you can use Promises together with async/await syntactic sugar, which makes asynchronous computation look like synchronous. AWS JavaScript SDK added a function promise() to almost all the functions previously using callbacks. Now you can write:

exports.handler = async event => {
    var params = {
        Bucket: "examplebucket", 
        Key: "HappyFace.jpg"
    };
    var data = await s3.getObject(params).promise();
    // here you process the already resolved data...
    return data;

    // or you can omit `await` here whatsoever:
    // return s3.getObject(params).promise();
}

哪种代码更简短,更优雅,更易于人类阅读.

Which produces shorter and eleganter code, more readable for humans.

当然,最后您可以选择自己喜欢的内容,只需比较示例片段...

Of course, at the end you can choose what you like, just compare your example snippets...

这篇关于从Node.js中的AWS Lambda函数返回的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆