LLL
LLL

Reputation: 3771

DynamoDB API Node, is it possible to get results synchroniously?

I know the normal way to do things is

db.query(params, callback);

But how and can I do something like

var data = db.query(params);

If the rest of my lambda can't execute before I get the results, can it just wait for it without having all the callbacks?

Upvotes: 1

Views: 1093

Answers (1)

notionquest
notionquest

Reputation: 39176

There is no synchronous way for Query api. However, you can use Promise for Query API.

Promises provide an alternative to the use of a callback function to manage asynchronous flow. They allow treating asynchronous calls as a variable, simplifying error handling and providing greater control over handling results from asynchronous calls.

AWS SDK support for promise

Sample code using Query API :-

var table = "Movies";

var year_val = 1992;
var title = "Movie with list attribute";

var params = {
    TableName: table,
    KeyConditionExpression: 'yearkey = :hkey and title = :rkey',
    ExpressionAttributeValues: {
        ':hkey': year_val,
        ':rkey': title        
    }
};

var queryObjectPromise = docClient.query(params).promise();

queryObjectPromise.then(function (data) {
    console.log("Query Item succeeded: ", JSON.stringify(data,
        null, 2));
}).catch(function (err) {
    console.log("Unable to read item. Error JSON: ", JSON.stringify(err, null, 2));

});

Upvotes: 4

Related Questions