Home > Net >  NodeJS API Async/Await issue: AWS SDK
NodeJS API Async/Await issue: AWS SDK

Time:08-05

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.

CodePudding user response:

if you want flag 1 and then flag 2 then here the things are that you missed into your code

first: getText() method should return Promise : make aws-sdk method in promisify way second: you need to receive values of get text that prints for flag 2 rather than undefined

How to make your code promisify and take value from the promisified function to use in another function/method

  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) {
  try {
  const text = await getText();
  console.log("Flag 2 : "   text);
  res.send("Hello World! : "   text);
  } catch (ex) {
  //do your error stuff here
   res.send("ERR: Translate error");
  }
 });

 app.listen(port, () => {
   console.log(`Example app listening on port ${port}`);
 });

  function getText() {
    var params = {
     SourceLanguageCode: "auto",
     TargetLanguageCode: "fr",
     Text: "Hello! Good day.",
   };
   
  // promisify your callback
  return new Promise((resolve, reject) => {
     translate.translateText(params, function (err, data) {
  if (err) {
    console.log(err, err.stack);
    reject(err);
  } else {
    console.log("Flag 1 : "   data.TranslatedText);
    resolve(data.TranslatedText);
  }
   });
  });
 }

you can promisify any function by using below mention way

 // promisify your callback
 return new Promise((resolve, reject) => {
 yourFunction(....., function (err, data) {
 if (err) {
 ....
  reject(err);
 } else {
 .....
 resolve(data);
}
 });
});
  • Related