Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!