如何使用 async/await 调用 AWS SDK 的 Athena API?

How do I call the AWS SDK's Athena API with async/await?

AWS SDK 的 Athena API 有一个带有此签名的函数:

startQueryExecution(params: Athena.Types.StartQueryExecutionInput, callback?: (err: AWSError, data: Athena.Types.StartQueryExecutionOutput) => void): Request<Athena.Types.StartQueryExecutionOutput, AWSError>;

这使用回调(第二个参数),所以我必须这样调用它:

athenaAPI.startQueryExecution(params, (err, data) => {...});

我更愿意这样称呼它:

const result = await startQueryExecution(params);

如何将此函数(可能使用节点的 promisify,但我不关心如何)转换为使用 promises 而不是回调?

我知道我输入的大部分内容都是无类型的 javascript,但我更喜欢使用带有打字稿的类型的答案。

谢谢!

在 JavaScript 的 AWS 开发工具包中,您可以将 .promise() 添加到任何 API 调用以获得承诺:

const response = await athenaAPI.startQueryExecution(params).promise()

您可以在此处阅读更多相关信息:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html