【发布时间】:2019-10-21 09:26:04
【问题描述】:
在下面的代码 sn-p 中,我使用 then 来获得承诺的结果。但是,response 不会返回。返回的是Promise { <pending> }。
我已经登录 response 并且可以看到它正确返回了数据,而不是未决的承诺。那么为什么它会返回一个未决的承诺呢?我什至在对describeTable 的调用中添加了then 调用,但它仍然处于等待状态。
我已阅读以下问题,但它们没有帮助,因此请不要将它们标记为重复:
How do I return the response from an asynchronous call?
async/await implicitly returns promise?
const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-west-2'});
const docClient = new AWS.DynamoDB;
async function describeTable() {
const params = {
TableName: 'weatherstation_test',
};
let response;
try {
response = await docClient.describeTable(params).promise().then(result => result);
console.log(response); // Logs the response data
} catch (e) {
console.error(e)
throw e;
}
return response;
}
console.log(describeTable().then(result => result)); // Logs Promise { <pending> }
更新
所以我删除了第一个then(在promise() 之后),因为它是多余的。 @libik 的答案对我有用。这是我不理解运行then 的上下文。
【问题讨论】:
-
如果你使用
await就不需要then构造。 -
尝试删除
.then(...)
标签: node.js promise aws-sdk-nodejs