简体   繁体   English

在由.fetch()、.all() 和.() 组成的promise 链中等待结果然后

[英]Waiting for result in promise chain consisting of .fetch(), .all() and .()then

I'm trying to write a Promise Chain.我正在尝试编写 Promise 链。 This is abstractly speaking what I want to do:抽象地说,这是我想做的事情:

  1. fetch() a record from an API. fetch()来自 API 的记录。 (My Item) (我的项目)
  2. then() Search in this record for more API links. then()在此记录中搜索更多 API 链接。 (Here I find the links to the media that belong to the item. Each item can have any number of media). (在这里我找到了属于该项目的媒体的链接。每个项目可以有任意数量的媒体)。
  3. all() Execute an API call for each of these links with .fetch() all()使用.fetch()对这些链接中的每一个执行 API 调用
  4. then() if all previous promises are fulfilled, execute the render function that depends on this data. then()如果之前的所有承诺都已实现,则执行依赖于此数据的渲染 function。

This is the code I have created for this so far.到目前为止,这是我为此创建的代码。

getItemWithAllMedia(request) {
    return fetch(request)
      .then((response) => {
        return response.json();
      })
      .then((data) => {
        console.log(data);
        return data;
      })
      .then((result) => {
        let media_links = Array.from(result["o:media"], (x) => x["@id"]);
        return media_links;
      })
      .then((media_links) => {
        return Promise.all(media_links.map((url) => fetch(url)));
      })
      .then((media_response) => {
        /// debugging results from promise.all
        console.log(media_response); // (2) [Response, Response]
        console.log(media_response[0]); // Response {type: "basic", url: "XXX", redirected: false, status: 200, ok: true, …}
        console.log(media_response[0].json()); // Promise {<pending>}

        /// desired function: all responses to json and push to an array
        media_response.forEach((element) => {
          media_data.push(element.json());
        });

        /// show created array for debugging
        console.log(media_data);
        return media_data;
      })

As you can see from the comments, it doesn't work.正如您从评论中看到的那样,它不起作用。 I only got back the underfilled Promise.我只取回了填充不足的 Promise。 With which my loop of course does not work.我的循环当然不起作用。 I then tried to append .then() .然后我尝试了 append .then .then() Both inside Promise.all() and after the .then() in which I assumed it.Promise.all()和我假设的 .then( .then()之后。 But this has the same result.但这具有相同的结果。 Unfortunately, I have not found a way how I can wait for the result.不幸的是,我还没有找到等待结果的方法。 I thought then should go back a step and read about Promises in general.我当时认为 go 应该退后一步,阅读有关 Promises 的一般信息。 Is it possible that I have to do this with await / async ?我是否有可能必须使用await / async来执行此操作? But then I have to write the complete function differently or?但是然后我必须以不同的方式编写完整的 function 还是? It would be great if someone knows if this can be done?如果有人知道这是否可以做到,那就太好了? Or should I restart with Await and Async?还是我应该使用 Await 和 Async 重新启动?

EDIT Spelling error in the variable and accordingly corrected the console output.编辑变量中的拼写错误并相应地更正了控制台 output。

Ignoring the fact that you have a spelling issue with media_reponse , you should really call .json() on the response as soon as you have it.忽略media_reponse存在拼写问题的事实,您应该在收到响应后立即调用 .json .json() So do:这样做:

  // ....
  .then((media_links) => {
    return Promise.all(media_links.map((url) => fetch(url).then(response => response.json()));
  })

This will produce a promise that resolves to all the data.这将产生一个解析所有数据的 promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM