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

216
Vistas
What is the correct behaviour of async await inside for loop with axios call?

I'm trying to call a zoom API for various meeting ids. But I'm unable to figure out the behaviour of async-await.

I have two functions:

  1. getDesiredRecordings: Filters recordings array for required recordings. While doing so, If the recording is valid I'm calling another function who returns me this meeting ids agenda.
  2. getMeetingAgenda: Intended to return agenda of current meeting Id.

This agenda is stored corresponding to the id in an object. But, The agenda is coming out to be undefined. I tried console.log(response.data.agenda); inside the second function and it consols the agenda but why all the meeting agenda is coming out to be undefined I'm unable to undestand. I think that the for loop doesn't wait for await getMeetingAgenda and I'm unable to figure out how to make the for loop wait. I also tried the ternary operator that too fails.

CODE:

let { meetingIds, requiredMeetings, meetingAgendas } = await getDesiredRecordings(recordings,config);
      
async function getDesiredRecordings(recordings, config) {
  try {
    let requiredMeetings = [];
    if (!recordings || !Object.keys(recordings).length) {
      return requiredMeetings;
    }
    let meetingIds = [];
    let meetingAgendas = {};
    let isMeetingValid = false;
    console.log(config)
    for (let i = 0; i < recordings.length; i++) {
      if (recordings[i] && recordings[i].topic && isRecordingValid(recordings[i].topic)) {
        requiredMeetings.push(recordings[i]);
        meetingIds.push(recordings[i].id);
      // meetingAgendas[recordings[i].id] = await getMeetingAgenda(recordings[i].id,config);
        isMeetingValid = true;
      }
      isMeetingValid ? meetingAgendas[recordings[i].id] = await getMeetingAgenda(recordings[i].id,config): "" ;
      console.log("getDesiredRecordings ID: ",recordings[i].id, meetingAgendas[recordings[i].id]);    //Prints after getMeetingAgends console(correct id,undefined)
      isMeetingValid = false;
    }
    console.log(meetingAgendas);   //All undefined
    return {
      "meetingIds": meetingIds,
      "requiredMeetings": requiredMeetings,
      "meetingAgendas" : meetingAgendas
    };
  }
  catch (e) {
    return Promise.reject(new Error(e));
  }
}


async function getMeetingAgenda(meetingId, config) {
  console.log(meetingId)
      await axios.get(`${Zoom_Agenda_End_Point}${meetingId}`, config)
      .then(function (response) {
        console.log(response.data.agenda);
        return response.data.agenda;
      })
      .catch(e=>{
        console.log("Axios error occured in getMeetingAgenda: ",e);
        return "";
      });
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Returning axios.get worked in getMeetingAgenda function. return axios.get...

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to wait for all promises to finish in recordings loop. Remove the for loop and use map function:

await Promise.all(recordings.map(async (recording) => {
  if (recording && recording.topic && isRecordingValid(recording.topic)) {
    requiredMeetings.push(recording);
    meetingIds.push(recording.id);
    // meetingAgendas[recording.id] = await getMeetingAgenda(recording.id,config);
    isMeetingValid = true;
  }
  isMeetingValid ? meetingAgendas[recording.id] = await getMeetingAgenda(recording.id,config): "" ;
  console.log("getDesiredRecordings ID: ",recording.id, meetingAgendas[recording.id]);    //Prints after getMeetingAgends console(correct id,undefined)
  isMeetingValid = false;
  return recording;
}));

console.log(meetingAgendas);

Also make your getMeetingAgenda function return a axios: return axios..

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