我需要获取几个文件,但我不知道如何访问请求url,一旦解决了请求url的问题:
paths = ['1.png', '2.png', '3.png']
for (const path of paths) {
fetch(path)
.then(resp => resp.blob())
.then(blob => {
console.log('path: ' + path) <- I need access to the fetch path here
})
.catch((error) => {
console.log(error)
})
}
当resp.blob()承诺被解析时,我需要访问获取路径。我怎样才能进入那里的抓取路径?
发布于 2022-07-19 04:01:26
下面,我模拟了在您展示的代码中使用的fetch
API的各个部分。你可以看到它按照你表达的期望起作用。每个单独的fetch
结果需要多长时间才能解决,这将决定控制台消息的顺序。(继续按"Run“键进行实验,看到不同的随机结果。)
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function delay (ms, value) {
return new Promise(resolve => setTimeout(() => resolve(value), ms));
}
// Fetching in this sim will take between 0 and 1500 ms
function fetch (url) {
return delay(
getRandomInt(0, 1500),
{
blob: () => delay(
getRandomInt(0, 50),
new Blob(),
),
},
)
}
paths = ['1.png', '2.png', '3.png'];
for (const path of paths) {
fetch(path)
.then(resp => resp.blob())
.then(blob => {
console.log('path: ' + path);
})
.catch((error) => {
console.log(error);
})
}
https://stackoverflow.com/questions/73030807
复制相似问题