I have a working asynchronous call to dynamoDB:
async getCostCenterTagFromTable() {
// Create the DynamoDB service object
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
var params = {
TableName: 'csv-table',
Key: {
'BusinessUse': {S: 'eWFM'}
},
ProjectionExpression: 'CostCenter'
};
try {
// Asynchronously retrieve value from DynamoDB
const costCenter = await ddb.getItem(params).promise();
return costCenter;
}catch (error) {
console.error(error);
}
}
I'm able to view the contents inside of the returned Promise just fine, but I just can not figure out how to remove the value from inside the then() block and return it to another function. My getCostCenterTagValue function needs to return a string. That string happily resides inside the promise's then() block. I need to take it out and return it, but it always comes back as undefined. I know this is an issue with the asynchronous nature of this call, and I've tried everything to get that value out of the then block to no avail. Any assistance would be greatly appreciated!
getCostCenterTagValue() {
var costCenter = this.getCostCenterTagFromTable();
costCenter.then(console.log);
//INFO { Item: { AutoTag_CostCenter: { S: '1099:802:000000' } } }
var costCenterString = costCenter.then(data => {
console.log("Cost Center Tag:" + data.Item.AutoTag_CostCenter.S);
//INFO Cost Center Tag:1099:802:000000
return data.Item.AutoTag_CostCenter.S;
}).catch(function(err) {
console.log(err);
});
console.log(costCenter.then())
return costCenterString(???); // or return costCenter.then(???)
}