Home > Enterprise >  Why is variable being calculated twice in an async promise?
Why is variable being calculated twice in an async promise?

Time:08-21

CURRENTLY

I have the following function IN AWS that utilise Textract SDK async functions:

async function textParse(config) {
  const AWS = require("aws-sdk");
  let textract = new AWS.Textract();

  let analysisResults;
  console.log("Before Try");
  try {
    console.log("Start Document Analysis");
    analysisResults = await textract
      .startDocumentAnalysis(config, function (err, data) {
        if (err) {
          console.log("There was an error during doc analysis...");
          console.log(err, err.stack);
        } // an error occurred
        else {
          console.log("Doc analysis successful!");
          console.log(data);
          return data;
        } // successful response
      })
      .promise();
  } catch (e) {
    throw "Doc Analysis Error!"   e;
  }
  let JobId = analysisResults["JobId"];
  let textractResult = await textract
    .getDocumentAnalysis({ JobId: JobId })
    .promise();

  console.log("End!")

  return textractResult;
}

ISSUE

The console log is showing that analysisResults is being calculated twice. Note Doc analysis successful! appearing twice in the logs.

INFO Before Try
INFO Start Document Analysis
INFO Doc analysis successful!
INFO { JobId: '3a1e00c9b5ca9123124hhfdfdsdd02f2053c38ec5249e822c9e95f' }
INFO Doc analysis successful!
INFO { JobId: '5ef298d3a9123124hhfdfdsddsdssdsdsdds689580642a6346' }
INFO End!

I've spent a few hours trying to debug this and I suspect it's something to do with how I'm handling promises.

Any ideas on what changes I need to make?

CodePudding user response:

Don't know the textract library, but this is what the OP code appears to be trying to say in a singular (promise, not callback) style.

async function textParse(config) {
  const AWS = require("aws-sdk");
  let textract = new AWS.Textract();
  console.log("Before Try");
  try {
    console.log("Start Document Analysis");
    let analysisResults = await textract
      .startDocumentAnalysis(config)
      .promise();
    console.log("Doc analysis successful!", analysisResults);
    let textractResult = await textract
      .getDocumentAnalysis({ JobId: analysisResults["JobId"] })
      .promise();  
    console.log("End!", textractResult);
    return textractResult;
  } catch (err) {
    console.log("There was an error during doc analysis...");
    console.log(err, err.stack);
  }
}

I worry about answering this way, because it doesn't seem to address the symptom described (two success logs), but even if there's another problem in the code, I think this is still a valid rewrite, form-wise.

CodePudding user response:

I think it writes it twice because of the .promise and your awaiting it, you should only use await or .promise.

async function textParse(config) {
  const AWS = require("aws-sdk");
  let textract = new AWS.Textract();

  let analysisResults;
  try {
    analysisResults = await textract
      .startDocumentAnalysis(config, function (err, data) {
        if (err) {
          console.log("There was an error during doc analysis...");
          console.log(err, err.stack);
        } // an error occurred
        else {
          console.log("Doc analysis successful!");
          console.log(data);
          return data;
        } // successful response
      })
      .promise(); // do not promise here
  } catch (e) {
    throw "Doc Analysis Error!"   e;
  }
  let JobId = analysisResults["JobId"];
  let textractResult = await textract
    .getDocumentAnalysis({ JobId: JobId }, function (err, data) {
      if (err) console.log(err, err.stack);
      // an error occurred
      else return console.log(data); // successful response
    })
    .promise();

  return textractResult
}
  • Related