Async function returning pending promise, despite use of await [duplicate]

非 Y 不嫁゛ 提交于 2019-12-25 00:34:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!