Javascript 第二个url中带有数组的嵌套获取
我有一个嵌套的fetch,其中第二个url应该从第一个结果数据中获取一个值(来自数组)。问题是,我需要为数组的每个实例运行第二个url:Javascript 第二个url中带有数组的嵌套获取,javascript,promise,nested,fetch,Javascript,Promise,Nested,Fetch,我有一个嵌套的fetch,其中第二个url应该从第一个结果数据中获取一个值(来自数组)。问题是,我需要为数组的每个实例运行第二个url: fetch(searchUrl) .then(function (response) { return response.json() }) .then(function (data) { searchLoader.style.display = "none"; for (le
fetch(searchUrl)
.then(function (response) {
return response.json()
})
.then(function (data) {
searchLoader.style.display = "none";
for (let i = 0; i < 10; i++) {
let stock = `${data[i].name}, (${data[i].symbol})`
listOfStocks.innerHTML += `<li class="list-group-item"><a href="./company.html?symbol=${data[i].symbol}">${stock}<a/></li>`
// console.log(data[i].symbol)
}
return fetch(`https://stock-exchange-dot-full-stack-course-services.ew.r.appspot.com/api/v3/company/profile/${data[i].symbol}`)
})
.then(function (response) {
console.log(response)
return response.json();
})
.then(function (data1) {
console.log(data1);
})
.catch(function (error) {
console.log('Requestfailed', error)
});
});
在一个循环中,但这当然不起作用。我还尝试将第二次获取放入初始循环,但也没有成功
谢谢您应该这样使用:
fetch(searchUrl)
.then(function (response) {
return response.json()
})
.then(function (data) {
searchLoader.style.display = "none";
for (let i = 0; i < 10; i++) {
let stock = `${data[i].name}, (${data[i].symbol})`
listOfStocks.innerHTML += `<li class="list-group-item"><a href="./company.html?symbol=${data[i].symbol}">${stock}<a/></li>`
// console.log(data[i].symbol)
}
return Promise.all(data.map(item => fetch(`https://stock-exchange-dot-full-stack-course-services.ew.r.appspot.com/api/v3/company/profile/${item.symbol}`)));
})
.then(function (response) {
console.log(response)
return Promise.all(response.map(x => x.json()));
})
.then(function (data1) {
console.log(data1);
})
.catch(function (error) {
console.log('Requestfailed', error)
});
fetch(搜索URL)
.然后(功能(响应){
返回response.json()
})
.then(功能(数据){
searchLoader.style.display=“无”;
for(设i=0;i<10;i++){
设stock=`${data[i].name},(${data[i].symbol})`
listOfStocks.innerHTML+=`${stock} `
//console.log(数据[i].symbol)
}
返回Promise.all(data.map)(item=>fetch(`https://stock-exchange-dot-full-stack-course-services.ew.r.appspot.com/api/v3/company/profile/${item.symbol}`);
})
.然后(功能(响应){
console.log(响应)
返回Promise.all(response.map(x=>x.json());
})
.then(函数(数据1){
console.log(data1);
})
.catch(函数(错误){
console.log('Requestfailed',错误)
});
您是否尝试过Promise.all?第二次获取甚至不在for循环中
fetch(searchUrl)
.then(function (response) {
return response.json()
})
.then(function (data) {
searchLoader.style.display = "none";
for (let i = 0; i < 10; i++) {
let stock = `${data[i].name}, (${data[i].symbol})`
listOfStocks.innerHTML += `<li class="list-group-item"><a href="./company.html?symbol=${data[i].symbol}">${stock}<a/></li>`
// console.log(data[i].symbol)
}
return Promise.all(data.map(item => fetch(`https://stock-exchange-dot-full-stack-course-services.ew.r.appspot.com/api/v3/company/profile/${item.symbol}`)));
})
.then(function (response) {
console.log(response)
return Promise.all(response.map(x => x.json()));
})
.then(function (data1) {
console.log(data1);
})
.catch(function (error) {
console.log('Requestfailed', error)
});