好吧,我自己已经解决了这个问题,希望这个答案对其他人有帮助:
首先,send()
方法不是同步调用,但它也不返回承诺。相反,您必须在Request
在发送之前对象,以便您可以适当地响应成功/失败状态。
我已经使用以下代码完成了此操作:
const stepFn = new StepFunctions({ apiVersion: "2016-11-23" });
const stage = process.env.AWS_STAGE;
const name = `${message.command}-${message.upc}-${message.accountName}-${stage}-${Date.now()}`;
const params: StepFunctions.StartExecutionInput = {
stateMachineArn: `arn:aws:states:us-east-1:837955377040:stateMachine:transportation-${stage}-steps`,
input: JSON.stringify(message),
name
};
const request = stepFn.startExecution(params);
// listen for success
request.on("extractData", req => {
console.info(
`startExecution request for step function was sent and validated, context sent was:\n`,
JSON.stringify(params, null, 2)
);
callback(null, {
statusCode: 200
});
});
// listen for error
request.on("error", (err, response) => {
console.warn(
`There was an error -- ${err.message} [${err.code}, ${
err.statusCode
}] -- that blocked the kickoff of the ${message.command} ITMS command for ${
message.upc
} UPC, ${message.accountName} account.`
);
callback(err.statusCode, {
message: err.message,
errors: [err]
});
});
// send request
request.send();
现在请记住有一个“成功”事件,但我使用“extractData”来捕获成功,因为我想尽快得到响应。成功也可能同样有效,但查看 Typescript 类型中的语言并不完全清楚,在我的测试中,我确信“extractData”方法确实按预期工作。
至于为什么我没有在我的步骤函数上得到任何执行......它必须按照我命名函数的方式......你仅限于名称中的字符子集,并且我已经超越了该限制,但是直到我能够使用上面的代码捕获错误时才意识到。