0

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');

    });
};
3
  • Have you tried returning scanPromise from the function. e.g. return scanPromise.then(.....) Commented Dec 8, 2018 at 1:36
  • Ok that does cause the promise to execute. Although it causes a dynamodb access failure in log without hitting the console.log line in my code. If you enter this in answer I can mark as answered. Commented Dec 8, 2018 at 1:48
  • I added it as an answer... Commented Dec 8, 2018 at 2:06

1 Answer 1

1

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(...)

Sign up to request clarification or add additional context in comments.

1 Comment

I have a similar issue, but this does not work for me. I've written it exactly as you suggested and posted a question here: stackoverflow.com/questions/55518992/… Could you provide any feedback?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.