我猜這是一個新手問題,但我遇到了一個問題,我想重復獲取資料的操作并向 api 重復發送請求,直到我收到的資料是我想要的具體資料,我該怎么做和我的代碼做錯了什么?到此為止,在此先感謝您的幫助!:)
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)}
uj5u.com熱心網友回復:
您可以使用async函式和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)
}
uj5u.com熱心網友回復:
您也可以撰寫一個if回圈,但如上所述,使用async& await。我個人if更喜歡回圈,但我想這并不重要。
async function diceBet() {
await ( .. // fetching here )
}
if(result < 99) diceBet() // call your function again until condition is met
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/380972.html
標籤:javascript 接口 拿来
上一篇:nodejs如何找到行程ID
