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)}
CodePudding user response:
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)
}
CodePudding user response:
You could also write a if
loop, but like mentioned above, with async
& await
. I personally like the if
loop more, but it does not really matter I suppose.
async function diceBet() {
await ( .. // fetching here )
}
if(result < 99) diceBet() // call your function again until condition is met