2 回答

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

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
我寧愿這樣做,創(chuàng)建一個(gè)IIFE,并為后續(xù)的獲取請(qǐng)求遞歸調(diào)用它:
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);
}
添加回答
舉報(bào)