异步函数返回挂起的Promise,尽管使用了await

时间:2019-10-21 09:01:39

标签: javascript node.js promise aws-sdk-nodejs

在下面的代码片段中,我试图理解为什么函数不返回promise的结果。

它首先记录Promise { <pending> },然后 用来自描述该表的AWS API的正确数据记录response变量。

为什么要这样做?我试过将函数包装在另一个异步函数中,等待它,然后得到相同的结果。

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> }

更新

即使我添加了Promise { <pending> },它仍然返回.then(result => result)

0 个答案:

没有答案