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

202
Vistas
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 Respuestas
Responde la pregunta

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 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