Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Run GET Request in While Loop or Similar

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
        });
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading