Javascript 如何在for循环中使用fetch,等待结果,然后将其记录到console.log中
我有这个问题:我想在for循环中进行多个fetch调用。调用的数量取决于用户输入(在我的示例中,我有三个)。我如何使它在所有提取请求中循环,然后在console.log中记录调用的次数 函数getPosts(){Javascript 如何在for循环中使用fetch,等待结果,然后将其记录到console.log中,javascript,fetch,Javascript,Fetch,我有这个问题:我想在for循环中进行多个fetch调用。调用的数量取决于用户输入(在我的示例中,我有三个)。我如何使它在所有提取请求中循环,然后在console.log中记录调用的次数 函数getPosts(){ let url=[”https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"]; 让数组=新数组; for(设i=0;i{return res.text();}) 。然后(res=>{ let
let url=[”https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"];
让数组=新数组;
for(设i=0;i{return res.text();})
。然后(res=>{
let reg=/\{return console.log(status,err);})
}
console.log(array.length);
}
它会记录console.0而不是3,因为它不会等待所有承诺都得到解决。如何才能将其记录到console.log 3?
如果您知道解决方案,请帮助我。您可以尝试链接您的承诺,请尝试以下操作: 函数getPosts(){ 让url=[”https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"]; 让数组=新数组; var promise=promise.resolve(); for(设i=0;i
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);
});
}
let url=[”https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"];
让数组=新数组;
var fetches=[];
for(设i=0;i{return res.text();})
。然后(res=>{
let reg=/\{return console.log(status,err);})
);
}
Promise.all(fetches).then(function(){
console.log(array.length);
});
}
Promise.all等待所有获取完成,然后它将打印#。您应该使用Promise,Promise是一个值的代理,在创建承诺时不一定知道该值。它允许您将处理程序与异步操作的最终成功值或失败原因相关联。这允许异步方法返回值,如同步方法:异步方法不是立即返回最终值,而是返回在将来某个时间点提供值的承诺
const fetch = require('node-fetch')
let url = ["https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"];
let array = new Array;
function get(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(res => { return res.text(); })
.then(res => {
let reg = /\<meta name="description" content\=\"(.+?)\"/;
res = res.match(reg);
resolve(res)
//console.log(res);
}
)
.catch(err => { reject(err) })
});
}
async function result() {
for (let i = 0; i < url.length; i++) {
const value = await get(url[i]);
array.push(value)
}
console.log(array.length)
}
result()
const fetch=require('node-fetch')
让url=[”https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"];
让数组=新数组;
函数get(url){
返回新承诺((解决、拒绝)=>{
获取(url)
.then(res=>{return res.text();})
。然后(res=>{
let reg=/\{reject(err)})
});
}
异步函数结果(){
for(设i=0;i
==>array.length=2,您可以使用异步等待(async:function,await:operator)。等待运算符只需等待承诺被解析。第一个承诺将被解析,然后它将移动到另一个承诺。此外,如果它在任何获取中发现错误,它将立即捕获错误。您可以将异步/等待与try/catch一起使用: 异步函数getPosts(){ 让数组=[]; 让url=[”https://www.freecodecamp.org", "https://www.test.de/", "http://www.test2.com"];
let reg=/\{console.log('done')});谢谢你的主意。它确实有效,但只有在所有提取请求都成功解决的情况下……如果其中一个提取请求失败,你知道我如何拥有console.log吗?谢谢你的输入。我该如何写这个承诺
const fetch = require('node-fetch')
let url = ["https://www.freecodecamp.org", "https://www.test.de/, http://www.test2.com"];
let array = new Array;
function get(url) {
return new Promise((resolve, reject) => {
fetch(url)
.then(res => { return res.text(); })
.then(res => {
let reg = /\<meta name="description" content\=\"(.+?)\"/;
res = res.match(reg);
resolve(res)
//console.log(res);
}
)
.catch(err => { reject(err) })
});
}
async function result() {
for (let i = 0; i < url.length; i++) {
const value = await get(url[i]);
array.push(value)
}
console.log(array.length)
}
result()