Reputation: 3771
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
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.
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