问题
In the below code snippet I am trying to understand why the function does not return the result of the promise.
It first logs Promise { <pending> }
, then logs the response
variable with the correct data from the AWS API describing the table.
Why is it doing this? I've tried wrapping the function in another async function and awaiting it and I get the same result.
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();
console.log(response); // This logs second. It logs the data correctly
} catch (e) {
console.error(e)
throw e;
}
return response;
}
console.log(describeTable()); // This logs first. It logs Promise { <pending> }
Update
This still returns Promise { <pending> }
even if I add .then(result => result)
.
来源:https://stackoverflow.com/questions/58482980/async-function-returning-pending-promise-despite-use-of-await