Step 函数在 JS 中不执行,但在 MJS Node 18.x+ 中执行

编程入门 行业动态 更新时间:2024-10-04 09:33:17

Step 函数在 JS 中不执行,<a href=https://www.elefans.com/category/jswz/34/1771274.html style=但在 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+ 中执行

本文发布于:2024-05-31 03:25:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但在   中不   函数   JS   Step

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!