从未执行过从dynamodb检索文档的异步代码

时间:2019-06-22 18:30:31

标签: javascript async-await aws-lambda

DynamoDB完成时应执行的代码永远不会执行。

即使我使用扫描的内置Promise,await也不等待扫描完成。即使手动添加其他await命令,它也不会执行。

    var AWS = require("aws-sdk");
    AWS.config.update({
          region: "us-west-2",
          endpoint: "http://localhost:8000"
        });
    var docClient = new AWS.DynamoDB.DocumentClient(); 
        var params = {
                TableName : "residenciaLN",            
            };


    exports.handler = async (event) => {
        console.log("Querying...");

        const load = async () => {
            console.log("Got in...");
            const { Items } = await docClient.scan(params).promise();
            const item = Items[0];
            console.log('Items: ' + item);
            return;
        };
        await load;   // no effect on this line being here or not
        console.log("Exiting...");   
        return 
    }; 

    Actual result:
    INFO    Querying...
    INFO    Exiting...

    Expected result: 
    INFO    Querying...
    INFO    Got in...
    INFO    Items: XXXXXX
    INFO    Exiting...

2 个答案:

答案 0 :(得分:1)

load是一个函数。要触发函数调用,您必须在其后使用括号。

要使其正常运行,请更改此行:

await load;

对此:

await load();

答案 1 :(得分:0)

您需要调用load作为函数 await load()代替await load

exports.handler = async (event) => {
    console.log("Querying...");

    const load = async () => {
        console.log("Got in...");
        const { Items } = await docClient.scan(params).promise();
        const item = Items[0];
        console.log('Items: ' + item);
        return;
    };
    await load();   // instead of [await load]
    console.log("Exiting...");   
    return 
};