0

The following code:

async function addData() {
    console.log("Adding a new record");
    var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });

    var params = {
        TableName: 'mytable-xxx',
        Item: {
            'name': { S: 'xxx },
            'vendor': { S: 'yy'}
        }
    };

    // Call DynamoDB to add the item to the table
    await ddb.putItem(params, function (err, data) {
        if (err) {
            console.log("addData Error", err);
        } else {
            console.log("addData Success", data);
        }
    });
    console.log("Finishing adding a new record");
}

And as a result i do get output:

Adding a new record
Finishing adding a new record

Why await did not work ? This i AWS Lambda execution.

Thanks,

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
user2913139
  • 557
  • 2
  • 5
  • 13

1 Answers1

1

Do not mix callback and promise. You can use ddb.put().promise() instead of the callback. See below:

let result = await ddb.put(params).promise();
console.log(result) 

And your code will look like this:

async function addData() {
    console.log("Adding a new record");
    var ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });

    var params = {
        TableName: 'mytable-xxx',
        Item: {
            'name': { S: 'xxx' },
            'vendor': { S: 'yy' }
        }
    };



    // Call DynamoDB to add the item to the table

    try {
        let result = await ddb.put(params).promise();
        console.log("addData Success", result);
    } catch (err) {
        console.log("addData Error", err);
    }

    console.log("Finishing adding a new record");
}
Arun Saini
  • 6,714
  • 1
  • 19
  • 22