【发布时间】:2021-08-22 15:33:54
【问题描述】:
我有一些 api 端点。
返回所有服务器详细信息 (https://dnscheck.io/api/serverDetails/)
其他是server specific 端点。 (https://dnscheck.io/api/query/?id=2&type=A&hostname=test.com) 对于每个 server_Id(我从 serverDetails 端点获得),我必须调用每个 api 端点。
我所做的是。
我循环遍历结果数组(我从serverDetails 端点获得)
对于循环的每次迭代,我都会调用每个端点来获取 ip。
循环:
for (const [index, item] of data.entries()) {
const res = await fetch(
`https://dnscheck.io/api/query/?id=${item.id}&type=${query.type}&hostname=${query.host}`
);
const result = await res.json();
renderResult(result, item, index);
}
渲染功能:
const renderResult = (result, data, index) => {
const ip = document.querySelector(`.ip-address${index + 1}`);
ip.innerHTML = result.answers[0].address;
};
通过这种方式,结果以同步的方式显示在 DOM 中。 (一个接一个)
但是,我想要的是,一旦结果准备好,就用结果更新 dom。
我能做什么?
【问题讨论】:
-
But, what I want is, update the dom with the result, as soon as the result is ready.你的意思是当你在for循环中发出的所有获取请求的结果s都准备好时? -
不是这样的。看看搜索结果是怎么来的。它们不同步。我要这个。
https://www.whatsmydns.net/
标签: javascript css api async-await dom-manipulation