In the below code snippet I am using then
to get the result of a promise. However, response
is not returned. What is returned is Promise { <pending> }
.
I've logged response
and can see it correctly returned the data, not a pending promise. So why is it returning a pending promise? I've even added a then
call to the call to describeTable
, and it still comes back pending.
I've read the following questions and they were not helpful, so please do not mark as a duplicate of them:
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> }
Update
So I've removed the first then
(after promise()
) because it is redundant. The answer from @libik works for me. It was the context in which then
is run I wasn't understanding.