【发布时间】:2022-01-13 19:40:32
【问题描述】:
我正在尝试将 PokeAPI 与 JS 一起使用。
我可以成功获取 Pokeman 信息并显示,但顺序不正确。
我使用了forEach 循环,并希望等到我添加第一个 Pokeman,然后添加第二个...
我发现了await,我尝试使用它,但我做不到。
有人可以帮帮我吗?
这是我的 JS 代码:
var url = 'https://pokeapi.co/api/v2/pokemon/';
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(response) {
response.results.forEach(element => {
var url = 'https://pokeapi.co/api/v2/pokemon/' + element.name;
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(response) {
addPokemon(response.name, response.sprites.front_default, response.types[0].type.name, response.height, response.weight);
})
});
})
【问题讨论】:
标签: javascript api loops foreach async-await