我有这个问题:我想在for循环中进行多次获取调用.调用次数取决于用户输入(在我的示例中,我有三个).如何让它循环遍历所有获取请求,然后console.log关闭号码?
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);
}
Run Code Online (Sandbox Code Playgroud)
它控制台.logs 0而不是3,因为它不等待所有的承诺得到解决.我怎样才能使它成为console.log 3?如果您知道解决方案,请帮帮我.