2 回答

TA貢獻1847條經驗 獲得超7個贊
你不能在循環中做抓取。您將返回完成的第一個抓取。使用 promise 或 await/async 在循環中獲取。如何在循環中返回許多承諾,并等待它們都做其他事情

TA貢獻1859條經驗 獲得超6個贊
我寧愿這樣做,創建一個IIFE,并為后續的獲取請求遞歸調用它:
return dispatch =>{
var ctr = 0;
(function myFunc(url, headerObj){
fetch(url, headerObj)
.then(response => {
response.json().then(data=>{
ctr++;
if(ctr ===1 ){ // This could be any condition, say, something on the basis of response; I have taken `ctr` as a condition
myFunc(url, { //You may change this even to different URL, if needed
method: 'POST',
headers: {
'content-type': 'application/json',
'body': ...,
'Authorization':...
}
});
}else if(ctr === 2){
myFunc(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
'body': ...,
'Authorization':...
}
});
}else{
// Any other code
}
})
})
})(url, headerObj);
}
添加回答
舉報