我必须在fetch上看到这样的照片:
for (const imgurl of urls){
fetch('https:/........url=' + imgurl)
.then(res => res.blob())
.then(blob => {
console.log(blob);
blobs.push(blob);
});当我将照片插入数组时,我希望保持for循环的顺序。这意味着,我在寻找一种方法:
fetch添加一个index,并在回调上读取它。当使用index获得回调时,将结果blob插入到blobs数组中的右index中。(如果我在2之前得到了索引4,将其放入4中,或稍后以某种方式保存和重新组织数组)假设数组大小未知,可以是5-9。
发布于 2020-05-11 17:42:55
使用Promise.all:
const fetches = [];
for (const imgurl of urls){
fetches.push(fetch('https:/........url=' + imgurl));
});
Promise.all(fetches).then(async results => {
for (const res of results) {
const blob = await res.blob();
console.log(blob);
blobs.push(blob);
}
});像这样,没有测试过。Promise.all保证在完成所有获取之后,以有效的顺序获得结果数组。
https://stackoverflow.com/questions/61735769
复制相似问题