承诺的 JS 代码不适用于 AWS lambda 但在更改为 async/await 样式时有效
JS code with promise not working on AWS lambda but works when changed to async/await style
我正在尝试在 AWS Lambda 上创建一个函数,以使用 Twilio API 将 SMS 发送到手机 phone。我正在使用 Node.JS 并且主要遵循此处 post 中的说明:https://www.twilio.com/blog/aws-lambda-layers-node-js-twilio-sms
我从那个post那里得到的发送短信的代码是这样的:
exports.handler = (event, context, callback) => {
// Your Account SID from www.twilio.com/console
// See http://twil.io/secure for important security information
const accountSid = process.env.ACCOUNT_SID;
// Your Auth Token from www.twilio.com/console
// See http://twil.io/secure for important security information
const authToken = process.env.AUTH_TOKEN;
// Import Twilio's Node Helper library
// Create an authenticated Twilio Client instance
const client = require('twilio')(accountSid, authToken);
// Send a text message
client.messages.create({
body: 'Hello from Lambda!',
to: '+12345678901', // your phone number
from: '+12345678901' // a valid Twilio number
})
.then((message) => {
// Success, return message SID
callback(null, message.sid);
})
.catch((e) => {
// Error, return error object
callback(Error(e));
});
};
当我在 AWS 上创建 lambda 时,我选择了一个 Hello-World 模板并将 exports.handler 回调标记为 async
,就像这样 exports.handler = async (event, context, callback) => {
。我没有注意到这个 async 关键字,而是从 post 中复制粘贴了方法主体,最终将 async/await
代码与 promise 风格的 .then
代码混合在一起。我首先从我的计算机本地测试了这段代码并且它起作用了,因为我只是使用了方法体而没有任何顶级函数或任何东西。但是当我从 AWS Lambda 中 运行 时,环境变量被正确记录,但之后我没有收到任何其他 O/P 或错误。在浪费了很多时间之后,我决定将链式承诺样式代码更改为 async/await 样式代码并且它起作用了。我修改后的代码是这样的:
exports.handler = async (event, context, callback) => {
// Your Account SID from www.twilio.com/console
// See http://twil.io/secure for important security information
const accountSid = process.env.ACCOUNT_SID;
// Your Auth Token from www.twilio.com/console
// See http://twil.io/secure for important security information
const authToken = process.env.AUTH_TOKEN;
// Import Twilio's Node Helper library
// Create an authenticated Twilio Client instance
const client = require('twilio')(accountSid, authToken);
try {
const messageSid = await client.messages.create({
body: 'Hello from Lambda!',
to: '+12345678901', // your phone number
from: '+12345678901' // a valid Twilio number
});
}
catch(err) {
callback(Error(err));
}
};
我的问题是,将 async/await 与 promise 类型代码(.then 构造)混合使用是否错误?如果我在第一行代码的第一行添加 async 关键字,它就不起作用。为什么会这样?
你不应该把两者结合起来。详细信息可以在这里找到:
AWS Lambda function handler in Node.js.
一个关键是:
For non-async handlers, function execution continues until the event loop is empty or the function times out. The response isn't sent to the invoker until all event loop tasks are finished. If the function times out, an error is returned instead. You can configure the runtime to send the response immediately by setting context.callbackWaitsForEmptyEventLoop to false.
当您不使用 async
时,它将等待事件循环完成。当您使用 async
时它不会执行此操作,因为它希望您一直使用 async
。
我正在尝试在 AWS Lambda 上创建一个函数,以使用 Twilio API 将 SMS 发送到手机 phone。我正在使用 Node.JS 并且主要遵循此处 post 中的说明:https://www.twilio.com/blog/aws-lambda-layers-node-js-twilio-sms
我从那个post那里得到的发送短信的代码是这样的:
exports.handler = (event, context, callback) => {
// Your Account SID from www.twilio.com/console
// See http://twil.io/secure for important security information
const accountSid = process.env.ACCOUNT_SID;
// Your Auth Token from www.twilio.com/console
// See http://twil.io/secure for important security information
const authToken = process.env.AUTH_TOKEN;
// Import Twilio's Node Helper library
// Create an authenticated Twilio Client instance
const client = require('twilio')(accountSid, authToken);
// Send a text message
client.messages.create({
body: 'Hello from Lambda!',
to: '+12345678901', // your phone number
from: '+12345678901' // a valid Twilio number
})
.then((message) => {
// Success, return message SID
callback(null, message.sid);
})
.catch((e) => {
// Error, return error object
callback(Error(e));
});
};
当我在 AWS 上创建 lambda 时,我选择了一个 Hello-World 模板并将 exports.handler 回调标记为 async
,就像这样 exports.handler = async (event, context, callback) => {
。我没有注意到这个 async 关键字,而是从 post 中复制粘贴了方法主体,最终将 async/await
代码与 promise 风格的 .then
代码混合在一起。我首先从我的计算机本地测试了这段代码并且它起作用了,因为我只是使用了方法体而没有任何顶级函数或任何东西。但是当我从 AWS Lambda 中 运行 时,环境变量被正确记录,但之后我没有收到任何其他 O/P 或错误。在浪费了很多时间之后,我决定将链式承诺样式代码更改为 async/await 样式代码并且它起作用了。我修改后的代码是这样的:
exports.handler = async (event, context, callback) => {
// Your Account SID from www.twilio.com/console
// See http://twil.io/secure for important security information
const accountSid = process.env.ACCOUNT_SID;
// Your Auth Token from www.twilio.com/console
// See http://twil.io/secure for important security information
const authToken = process.env.AUTH_TOKEN;
// Import Twilio's Node Helper library
// Create an authenticated Twilio Client instance
const client = require('twilio')(accountSid, authToken);
try {
const messageSid = await client.messages.create({
body: 'Hello from Lambda!',
to: '+12345678901', // your phone number
from: '+12345678901' // a valid Twilio number
});
}
catch(err) {
callback(Error(err));
}
};
我的问题是,将 async/await 与 promise 类型代码(.then 构造)混合使用是否错误?如果我在第一行代码的第一行添加 async 关键字,它就不起作用。为什么会这样?
你不应该把两者结合起来。详细信息可以在这里找到: AWS Lambda function handler in Node.js.
一个关键是:
For non-async handlers, function execution continues until the event loop is empty or the function times out. The response isn't sent to the invoker until all event loop tasks are finished. If the function times out, an error is returned instead. You can configure the runtime to send the response immediately by setting context.callbackWaitsForEmptyEventLoop to false.
当您不使用 async
时,它将等待事件循环完成。当您使用 async
时它不会执行此操作,因为它希望您一直使用 async
。