Thank you for taking time to read this post. I am struggling to get AWS API call working with nodejs API using await.
Below is my sample application which when run without the AWS SDK call works as expected. Await call work and Flags in console.log are printed in sequence.
var AWS = require("aws-sdk");
const express = require('express')
const app = express()
const port = 8080
AWS.config.update({region: "us-west-2"});
var translate = new AWS.Translate();
app.get('/', async function (req, res){
var tt= await getText();
console.log("Flag 2 : "+tt);
res.send('Hello World! : '+ tt)
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
function getText()
{
console.log("Flag 1 : success");
return "data";// successful response
}
Output:
Debugger attached.
Example app listening on port 8080
Flag 1 : success
Flag 2 : data
================
When I add AWS SDK API call to the mix everything goes for a toss.
var AWS = require("aws-sdk");
const express = require('express')
const app = express()
const port = 8080
AWS.config.update({region: "us-west-2"});
var translate = new AWS.Translate();
var text ;
app.get('/', async function (req, res){
await getText();
console.log("Flag 2 : "+ text);
res.send('Hello World! : '+ text);
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
function getText()
{
var params = {
SourceLanguageCode: 'auto',
TargetLanguageCode: 'fr',
Text: 'Hello! Good day.'
};
translate.translateText(params, function (err, data) {
if (err) console.log(err, err.stack);
else console.log("Flag 1 : "+data.TranslatedText);
return data.TranslatedText;
});
}
Output:
Debugger attached.
Example app listening on port 8080
Flag 2 : undefined
Flag 1 : Bonjour ! Bonne journée.
===========
Flags are coming in incorrect order and Flag 2 is null. How do I pass the value from AWS SDK API call back to my parent function so I can return that in the API response.