我正在尝试访问 Amazon Comprehend API 来检测句子的情绪分数。但我不知道如何从回调中正确访问 API 调用后的结果:
function getSentiment(text){
console.log('old params.Text: '+JSON.stringify(params.Text));
params.Text = text;
console.log('new params.Text: '+JSON.stringify(params.Text));
var result = comprehend.detectSentiment(params,function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else sentiment = data.Sentiment; // successful response -> String mit Sentiment | "POSITIVE" "NEGATIVE" "NEUTRAL" "MIXED"
});
console.log('reslut of comprehend.detectSentiment: '+JSON.stringify(result));
console.log('sentiment from the data object: '+JSON.stringify(sentiment));
return sentiment;
}
如何存储 JavaScript 回调函数的结果?