Run GET Request in While Loop or Similar

Advertisements

basically i want to create an loop that makes GET Requests until a Condition is there and then use the Data from the GET Request.

I got this code:

checkAndAcceptPickup(23);


async function checkAndAcceptPickup(id: number) {
    while (!isDone) {
        getPickupsFromStore(id).then(data => {
            console.log(data);
        });
    }
}


async function getPickupsFromStore(id: number) {
    const response = await axios.get(`URL`)
    return response.data
}

But unfortunately nothing gets console logged, what am i doing wrong?

I tried to use a setInterval Method but that was scuffed too because of the await in the GET Request.

It should do:

Wait for GET Request => Check Condition => New GET Request….

>Solution :

You need to wait for the getPickupsFromStore function to complete before continuing the while loop.

It should look like this:

async function checkAndAcceptPickup(id: number) {
    while (!isDone) {
        await getPickupsFromStore(id).then(data => {
            console.log(data);
        });
    }
}

Leave a ReplyCancel reply