0👍
✅
In below code:
async created() {
this.prepairPokeIds() // sends a request and executes line below
await this.test()
console.log(this.pokemons)
console.log(this.types)
}
this.prepairPokeIds()
will fire off the first request in loop, control comes back to created
and it’ll execute await this.test()
which immediately executes console.log(this.types.length)
, so you get 0 which is the value at the time.
Code below should log proper value since it executes after first request is resolved.
async prepairPokeIds() {
for (let i = 1; i <= this.numOfPokemon; i++){
await this.fetchPokemonData(i)
console.log(this.types.length)
}
},
Or you can return a promise(s) from prepairPokeIds()
and await it before executing this.test()
Source:stackexchange.com