Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

217
Vistas
Why is variable being calculated twice in an async promise?

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?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda