首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >解析resp.blob()承诺时对获取路径的访问

解析resp.blob()承诺时对获取路径的访问
EN

Stack Overflow用户
提问于 2022-07-19 03:35:21
回答 1查看 27关注 0票数 0

我需要获取几个文件,但我不知道如何访问请求url,一旦解决了请求url的问题:

代码语言:javascript
运行
复制
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()承诺被解析时,我需要访问获取路径。我怎样才能进入那里的抓取路径?

EN

回答 1

Stack Overflow用户

发布于 2022-07-19 04:01:26

下面,我模拟了在您展示的代码中使用的fetch API的各个部分。你可以看到它按照你表达的期望起作用。每个单独的fetch结果需要多长时间才能解决,这将决定控制台消息的顺序。(继续按"Run“键进行实验,看到不同的随机结果。)

代码语言:javascript
运行
复制
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);
    })
}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73030807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档