I'm new to js - I'm having trouble wrapping my head around how to return a value from an asynchronous operation using a callback. This is my current iteration of my code and it is still returning 'test undefined'. Can anyone check what I'm doing wrong? Thank you. Any help appreciated.
var test = this.getImgurClientId(function (data) {
console.log(data.Item.ClientId.S); //this has a value
return data.Item.ClientId.S;
});
console.log('test ' + test); //prints 'test undefined'
this.getImgurClientId = function(callback) {
AWS.config.update({
accessKeyId: AWS_ACCESSKEYID,
secretAccessKey: AWS_SECRET_ACCESSKEYID,
region: AWS_DYNAMODB_REGION
});
var dynamodb = new AWS.DynamoDB();
//console.log(dynamodb);
var params = {
AttributesToGet: [
"ClientId"
],
TableName: 'ServiceProvider',
Key: {
"ProviderName": {
"S": "Imgur"
}
}
};
dynamodb.getItem(params, function(err, data) {
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
callback(err);
} else {
//this query succeeds
console.log("Query succeeded. " + JSON.stringify(data, null, 2));
callback(data);
}
});
}