Search code examples
node.jsaxiosget

Send multiple get requests with axios in nodejs


Initially I need to send more than thousand requests. But this will fail without pausing between requests. Since I am new to nodejs I don't know how to handle this.. Here is my current code:

async getParticipantInfos(id) {
    const url = 'https://..../'+id+'?..=..';

    try {
        const resp = await axios.get(url, {
            headers: {
                'Accept': 'application/json',
                'Authorization': 'Bearer '+this.accessToken
            }
        });
        return setTimeout(() => resp.data.participant, 1000);
    } catch(err) {
        console.log(err);
    }

}

await Promise.all(participants.map(async (p) => {
        // only imports participants with the state ID of 1 = Online!
        if(p.participantStateId === 1) {
            
            const participantInfos = await this.getParticipantInfos(p.id);
            //console.log(participantInfos);
            // make req to get more info about person
            let participant = {
                firstname: p.firstName,
                lastname: p.lastName,
                organisation: p.organisation,
                email: p.email,
                //code: participantInfos.vigenere2Code,
                participationType: p.participantTypeId,
                thirdPartyId: p.id,
                prefix: p.degree
            }
             // if participant === speaker then insert them into presentesr table as well
            if(p.participantTypeId === 2) {
                let speaker = {
                    id: p.id,
                    firstname: p.firstName,
                    lastname: p.lastName,
                    organisation: p.organisation,
                    thirdPartyId: p.id,
                    prefix: p.degree
                }
                speakers.push(speaker);
            }
            newParticipants.push(participant);

            //await new Promise(r => setTimeout(r, 1000));
        }
        console.log('q');
    }));

I tried to integrate a sleeper. But it just didnt work. It was not pausing between requests


Solution

  • When you use Promise.all([...]), all requests will run at the same time.

    If you want them to happen in sequence, you will need to do something like this:

    async function allRequests(participants) {
        const onlineParticipants = participant.filter(p => p.participantStateId === 1);
        for (participant of onlineParticipants) {
          const result = await getParticipantInfos(participant.id);
          // rest of code here...
        }
    }
    

    If you want to "sleep" between requests, you can do the following:

    async function allRequests(participants) {
        const onlineParticipants = participant.filter(p => p.participantStateId === 1);
        for (participant of onlineParticipants) {
          const result = await getParticipantInfos(participant.id);
          // rest of code here...
          await new Promise((resolve) => setTimeout(() => resolve(), 1000));
        }
    }
    

    Where 1000 is 1 second "sleep".