异步等待提取
我的获取与。然后,我想通过使用async
和等待
来提高档位。它应该等待所有5个API呼叫,然后放置答案,而是在每个API呼叫上显示答案
async function getPhotosFromAPI() {
for (let i = 1; i <= 5; i++) {
let albums = await fetch(
`https://jsonplaceholder.typicode.com/photos/?albumId=${i}`
);
let result = await albums.json();
let res = `<div class="album${i}"></div>`;
document.querySelector(".display-images").innerHTML += res;
for (let j = 1; j <= 5; j++) {
document.querySelector(
`.album${i}`
).innerHTML += `<img src="${result[j].url}"/>`;
}
}
console.log(result);
}
async function showPhotos() {
await getPhotosFromAPI();
document.getElementById("#loader").style.display = "none";
}
showPhotos();
document.getElementById("img").style.display = "block";
for (let i = 1; i <= 5; i++) {
fetch(`https://jsonplaceholder.typicode.com/photos/?albumId=${i}`)
.then((response) => response.json())
.then((json) => {
document.getElementById("img").style.display = "none";
const display = document.querySelector(".display-images");
const albumNo = document.querySelector(".album-no");
// document.getElementById('img').style.display = "block";
// document.getElementById('img').style.display = "none";]
display.innerHTML += `<div class="album-${i}>`;
for (let z = 1; z <= 5; z++) {
display.innerHTML += `<img id="img" alt="pic-from-album${json[i].albumId}" src="${json[z].url}"/>`;
}
display.innerHTML += `<div>`;
});
}
my fetch works perfectly with .then, but i want to step it up a notch by using async
and await
. It should wait for all 5 API calls, and then place the answer, instead, it shows answer on every API call
async function getPhotosFromAPI() {
for (let i = 1; i <= 5; i++) {
let albums = await fetch(
`https://jsonplaceholder.typicode.com/photos/?albumId=${i}`
);
let result = await albums.json();
let res = `<div class="album${i}"></div>`;
document.querySelector(".display-images").innerHTML += res;
for (let j = 1; j <= 5; j++) {
document.querySelector(
`.album${i}`
).innerHTML += `<img src="${result[j].url}"/>`;
}
}
console.log(result);
}
async function showPhotos() {
await getPhotosFromAPI();
document.getElementById("#loader").style.display = "none";
}
showPhotos();
document.getElementById("img").style.display = "block";
for (let i = 1; i <= 5; i++) {
fetch(`https://jsonplaceholder.typicode.com/photos/?albumId=${i}`)
.then((response) => response.json())
.then((json) => {
document.getElementById("img").style.display = "none";
const display = document.querySelector(".display-images");
const albumNo = document.querySelector(".album-no");
// document.getElementById('img').style.display = "block";
// document.getElementById('img').style.display = "none";]
display.innerHTML += `<div class="album-${i}>`;
for (let z = 1; z <= 5; z++) {
display.innerHTML += `<img id="img" alt="pic-from-album${json[i].albumId}" src="${json[z].url}"/>`;
}
display.innerHTML += `<div>`;
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用一种同时提取的方式,例如承诺。
You should use a concurrent way of fetching like Promise.all to avoid round-trips
您是您要求代码等到每个
fetch
通过使用等待
onfetch
的返回值(然后再次在返回值上,JSON
)在您的循环中。因此,它将做到这一点:等到该请求完成,然后再进行下一个循环迭代。如果您不想这样做,则需要一个接一个地启动每个
Fetch
,然后等待所有人完成。我可能只将其中一个用于一个功能,然后将其称为五次,建立返回的承诺的数组,然后等待Promise.all(/*....*/)< /code>那些承诺,沿着这些行:(
我以
。
。 ],它包含解析 JSON的结果。
You're asking for the code to wait until each
fetch
finishes by usingawait
onfetch
's return value (then again on the return value ofjson
) in your loop. So it will do just that: wait until that request is complete before moving on to the next loop iteration.If you don't want to do that, you need to start each
fetch
one after another and then wait for them all to complete. I'd probably break out the work for just one of them into a function, then call it five times, building an array of the promises it returns, thenawait Promise.all(/*...*/)
those promises, something along these lines:(I took the version with
.then
as my starting point for the above, since the two versions in your question were so different and you said the one with.then
worked... Also note that I renamed the variablejson
todata
, since it doesn't contain JSON [it's not a string], it contains the result of parsing JSON.)