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

195
Visualizações
Wait for map to finish and then write a log with Promise

I am working on a simple application that will go through DialogFlow (Google) and send me the intents that were picked up based on the users comment.

I have the following code.

let writeToExcel = [];



 var promises = xlData.map(data => {
   detectIntentText(data.QUESTION);
 });

 Promise.all(promises).then(function(results) {
   console.log(results);
   console.log(writeToExcel);
 })

However, the array that prints is all undefined, and it prints before the function, detectIntentText, finished everything in the map. There could be upwards of 1000 entries.

What is the best way to get this to work?

Here is the detectIntentText function

async function detectIntentText(userText) {
  const sessionId = Math.random().toString(36).substring(7);
  const sessionPath = client.projectLocationAgentSessionPath(
    projectId,
    location,
    agentId,
    sessionId
  );
  console.info(sessionPath);

  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        text: userText,
      },
      languageCode,
    },
  };
  const [response] = await client.detectIntent(request);
  // console.log(`User Query: ${userText}`);
  var intent = "";
  for (const message of response.queryResult.responseMessages) {
    if (message.text) {
      // console.log(`Agent Response: ${message.text.text}`);
    }
  }
  if (response.queryResult.match.intent) {
    // console.log(
    //   `Matched Intent: ${response.queryResult.match.intent.displayName}`
    //);
    intent = response.queryResult.match.intent.displayName;
  }
  // console.log(
  //   `Current Page: ${response.queryResult.currentPage.displayName}`
  // );

  let jsonToWrite = {
    CX_QUESTION: userText,
    INTENT: intent
  }

  // console.log(jsonToWrite);

  writeToExcel.push(jsonToWrite);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You are not returning anything from map. If detectIntentText() returns Promise, Then your code should be

 var promises = xlData.map(data => {
   return detectIntentText(data.QUESTION);
 });

 Promise.all(promises).then(function(results) {
   console.log(results);
   console.log(writeToExcel);
 })

You can get rid of the return keyword like this,

var promises = xlData.map(data => detectIntentText(data.QUESTION));
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