jhurtas
jhurtas

Reputation: 684

AWS Lambda using node.js not executing promise function

I created this simple function with two console.log calls. "After promise created" shown in log, while "scan promise executed" does not appear. I am new to node.js. Where is my mistake?

exports.handler = async(event, context) => {

    var AWS = require("aws-sdk");

    AWS.config.update({
        region: "us-east-1",
        endpoint: "https://dynamodb.us-east-1.amazonaws.com"
    });

    var docClient = new AWS.DynamoDB.DocumentClient();



    var params = {
        TableName: "User",
        KeyConditionExpression: null,
        FilterExpression: 'CusomerId = :customerid',

    };



    var scanPromise=docClient.scan(params).promise();
    console.log('\r\nAfter promise created');
    scanPromise.then(function(err,data)
    {
         console.log('\r\nScan promise executed');

    });
};

Upvotes: 0

Views: 1051

Answers (1)

euvs
euvs

Reputation: 1645

Your handler is an asynchronous function. If you don't return a promise, the handler will be executed but it will quit immediately without waiting for your promise to finish.

Change it to return scanPromise.then(...)

Upvotes: 1

Related Questions