<分区>
有了一组 Pokemon id,我希望通过从 Pokemon API 获取一组 Pokemon 对象。目标是从中得到
[1,2,3]
为此:
[
{name: "ivysaur", weight: 130, …},
{name: "venusaur", weight: 1000, …},
{name: "bulbasaur", weight: 69, …}
]
我仔细看了这个帖子 ' How can I fetch an array of urls with Promise.all ' 但没有一个解决方案对我有用。我有同样的问题outlined in this answer , 但提出的解决方案仍然没有产生结果。
我的 Promise.all
是用一组 undefined
来实现的,而不是口袋妖怪。为什么?
const pokemonIds = [1,2,3]
const pokemons = pokemonIds.map(id => {
fetch(`https://pokeapi.co/api/v2/pokemon/${id}`)
.then(res => res.json())
.then(json => {
console.log(json)
return json
})
})
Promise.all(pokemons).then(res => {
console.log('pokemon arr: ', res)
})