Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda