Home > Enterprise >  What is the correct behaviour of async await inside for loop with axios call?
What is the correct behaviour of async await inside for loop with axios call?

Time:09-24

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 "";
      });
}

CodePudding user response:

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

CodePudding user response:

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

  • Related