• <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 它

        How to use fetch within a for-loop, wait for results and then console.log it(如何在 for 循环中使用 fetch,等待结果,然后 console.log 它)
          <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 它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass
                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

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

                        <tbody id='aVNG0'></tbody>
                      • <bdo id='aVNG0'></bdo><ul id='aVNG0'></ul>
                        <tfoot id='aVNG0'></tfoot>
                      • <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>