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

How do i repeat this function to call the api depending on the value that is returned to me ? Javascript

this is a newbie question i guess but i am having an issue which i want to repeat the action of fetching data and making a post request to an api repeated times until the data i receive is the specific i want, how can i do that and what i am doing wrong with my code ? here is it so far, thanks in advance for any help ! 🙂

function diceBet(){
   
    do {
    fetch(url, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            "Accept": "application/json",
            "x-access-token": api,
        },
        body: JSON.stringify(
            bodyReq
        )}).then(function(resp){
            return resp.json()
        }).then(function(respData){
                console.log(respData);
            })
            
    }while(respData.data.diceRoll.result <= 99)}

>Solution :

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

You can use an async function and await.

async function diceBet() {
    let respData;
    do {
        respData = await (await fetch(url, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json",
                "x-access-token": api,
            },
            body: JSON.stringify(
                bodyReq
            )
        })).json();
    } while (respData.data.diceRoll.result <= 99)
}
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