但在 MJS Node 18.x+ 中执行"/>
Step 函数在 JS 中不执行,但在 MJS Node 18.x+ 中执行
有没有人知道为什么这段代码在这里:
import { SFNClient, StartExecutionCommand } from "@aws-sdk/client-sfn";
export const handler = async(event) => {
const ev = {
"txid": "8787878787877v423-98vh349hv34878787878787879",
"sid": "8ac36b76-e102-5e1b-ad94-5bb5e6564956",
"val": "80.0",
"cur": "USD",
"type": "s"
}
const stepFunctions = new SFNClient({region: 'us-east-1'});
let params = { input: JSON.stringify(ev), stateMachineArn: 'arn:aws:states:us-east-1:xxxxx'};
const command = new StartExecutionCommand(params);
await stepFunctions.send(command)
.then((result) => console.log(result))
.catch((er) => console.error(er.stack))
};
完美执行步骤功能,但是此代码(包含在运行节点 18.x 的常规 JS 文件中也会超时?[10 秒+])
const { SFNClient, StartExecutionCommand } = require("@aws-sdk/client-sfn");
exports.handler = async (event) => {
const ev = {
"txid": "8787878787877v423-98vh349hv34878787878787879",
"sid": "8ac36b76-e102-5e1b-ad94-5bb5e6564956",
"val": "80.0",
"cur": "USD",
"type": "s"
}
let params = { input: JSON.stringify(ev), stateMachineArn: 'arn:aws:states:us-east-1:us-east-1:xxxxx'};
const command = new StartExecutionCommand(params);
await stepFunctions.send(command)
.then((result) => console.log(result))
.catch((er) => console.error(er.stack))
}
两个 lambda 函数都有足够的权限,尽管后一个代码片段是一个更大函数的一部分,但所有先前的代码都能在几秒钟内正确执行。
我完全不知所措,一整天都在试图弄清楚为什么会这样。
任何建议将不胜感激!
回答如下:我认为您的代码在这两种解决方案中都是错误的。但我不能说为什么它在第一个有效而在第二个无效。
await stepFunctions.send(command)
.then((result) => console.log(result))
.catch((er) => console.error(er.stack))
试着这样写
try {
const result = await stepFunctions.send(command);
} catch (e) {
console.error(e);
}
您应该
await
承诺,或者在非异步函数中使用 then
和 catch
回调
更多推荐
Step 函数在 JS 中不执行,但在 MJS Node 18.x+ 中执行
发布评论