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);
}
添加回答
举报