• <legend id='9JWq9'><style id='9JWq9'><dir id='9JWq9'><q id='9JWq9'></q></dir></style></legend>
      <i id='9JWq9'><tr id='9JWq9'><dt id='9JWq9'><q id='9JWq9'><span id='9JWq9'><b id='9JWq9'><form id='9JWq9'><ins id='9JWq9'></ins><ul id='9JWq9'></ul><sub id='9JWq9'></sub></form><legend id='9JWq9'></legend><bdo id='9JWq9'><pre id='9JWq9'><center id='9JWq9'></center></pre></bdo></b><th id='9JWq9'></th></span></q></dt></tr></i><div id='9JWq9'><tfoot id='9JWq9'></tfoot><dl id='9JWq9'><fieldset id='9JWq9'></fieldset></dl></div>

        <small id='9JWq9'></small><noframes id='9JWq9'>

      1. <tfoot id='9JWq9'></tfoot>
        • <bdo id='9JWq9'></bdo><ul id='9JWq9'></ul>

        如何在 for 循环中使用 fetch,等待结果,然后 console.log 它

        时间:2023-10-02
          <tbody id='VRfAk'></tbody>
            <bdo id='VRfAk'></bdo><ul id='VRfAk'></ul>

              • <i id='VRfAk'><tr id='VRfAk'><dt id='VRfAk'><q id='VRfAk'><span id='VRfAk'><b id='VRfAk'><form id='VRfAk'><ins id='VRfAk'></ins><ul id='VRfAk'></ul><sub id='VRfAk'></sub></form><legend id='VRfAk'></legend><bdo id='VRfAk'><pre id='VRfAk'><center id='VRfAk'></center></pre></bdo></b><th id='VRfAk'></th></span></q></dt></tr></i><div id='VRfAk'><tfoot id='VRfAk'></tfoot><dl id='VRfAk'><fieldset id='VRfAk'></fieldset></dl></div>

                <small id='VRfAk'></small><noframes id='VRfAk'>

                <legend id='VRfAk'><style id='VRfAk'><dir id='VRfAk'><q id='VRfAk'></q></dir></style></legend>

                <tfoot id='VRfAk'></tfoot>
                1. 本文介绍了如何在 for 循环中使用 fetch,等待结果,然后 console.log 它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这个问题:我想在一个 for 循环中进行多次 fetch 调用.调用次数取决于用户输入(在我的示例中,我有三个).我怎样才能让它遍历所有的 fetch 请求,然后 console.log 关闭电话号码?

                  i have this problem: i want to make multiple fetch calls within a for-loop. The number of calls depend on the user input (in my example i have three). How can i make it loop through all the fetch requests and then console.log the number off calls?

                  函数 getPosts(){

                  function getPosts(){

                    let url = ["https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"];
                    let array = new Array;
                  
                    for (let i = 0; i < url.length; i++) {
                      console.log(url[i]);
                      fetch(url[i])
                      .then(res => {return res.text(); })
                      .then(res => {
                              let reg = /<meta name="description" content="(.+?)"/;
                              res = res.match(reg);
                              array.push(res);
                              console.log(res);
                            }
                      )
                      .catch(status, err => {return console.log(status, err);})
                    }
                    console.log (array.length);
                    }
                  

                  它 console.logs 是 0 而不是 3,因为它不会等待所有的 Promise 都被解决.我怎样才能使它成为console.log 3?如果您知道解决方案,请帮助我.

                  It console.logs 0 instead of 3, cause it doesn't wait for all the promises to be resolved. How can i make it to console.log 3? If you know a solution, please help me out.

                  推荐答案

                  你不能调用console.log(array.length),直到之后所有的promise都完成了.那么为什么不这样呢?

                  You can't call console.log(array.length) until after the promises are all done. So why not something like this?

                  let url = ["https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"];
                    let array = new Array;
                    var fetches = [];
                    for (let i = 0; i < url.length; i++) {
                      console.log(url[i]);
                      fetches.push(
                        fetch(url[i])
                        .then(res => {return res.text(); })
                        .then(res => {
                              let reg = /<meta name="description" content="(.+?)"/;
                              res = res.match(reg);
                              array.push(res);
                              console.log(res);
                            }
                        )
                        .catch(status, err => {return console.log(status, err);})
                      );
                    }
                    Promise.all(fetches).then(function() {
                      console.log (array.length);
                    });
                    }
                  

                  Promise.all 等待所有提取完成,然后它会打印 #.

                  Promise.all waits for all the fetches to finish, THEN it'll print the #.

                  这篇关于如何在 for 循环中使用 fetch,等待结果,然后 console.log 它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何处理来自“不透明"类型的获取响应? 下一篇:fetch 函数返回 Promise &lt;pending&gt;

                  相关文章

                2. <small id='aVNG0'></small><noframes id='aVNG0'>

                  • <bdo id='aVNG0'></bdo><ul id='aVNG0'></ul>
                  <tfoot id='aVNG0'></tfoot>
                3. <legend id='aVNG0'><style id='aVNG0'><dir id='aVNG0'><q id='aVNG0'></q></dir></style></legend>

                      <i id='aVNG0'><tr id='aVNG0'><dt id='aVNG0'><q id='aVNG0'><span id='aVNG0'><b id='aVNG0'><form id='aVNG0'><ins id='aVNG0'></ins><ul id='aVNG0'></ul><sub id='aVNG0'></sub></form><legend id='aVNG0'></legend><bdo id='aVNG0'><pre id='aVNG0'><center id='aVNG0'></center></pre></bdo></b><th id='aVNG0'></th></span></q></dt></tr></i><div id='aVNG0'><tfoot id='aVNG0'></tfoot><dl id='aVNG0'><fieldset id='aVNG0'></fieldset></dl></div>