This is a great write up of a common problem, I would even go one step further and suggest that for your second example could be improved even more
asyncfunctionwithAwaitAsyncData(){constresponsePokemon=awaitaxios('https://pokeapi.co/api/v2');constresponseDigimon=axios('https://digimon-api.herokuapp.com/api/digimon');// do pokemon stuff// do digimon stuff}
asyncfunctionwithAwaitAsyncData(){constresponsePokemon=axios('https://pokeapi.co/api/v2');constresponseDigimon=axios('https://digimon-api.herokuapp.com/api/digimon');awaitresponsePokemon;// do pokemon stuffawaitresponseDigimon// do digimon stuff}
Since the request to the digimon API doesn’t rely on the pokemon API response, you could trigger them separately and only await on the Promise once you need it.
Thanks for clarifying it. That was exactly what I was trying to say, If you don't need it that exact moment you do not have to await it. This is one of pitfalls of async/await.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This is a great write up of a common problem, I would even go one step further and suggest that for your second example could be improved even more
Since the request to the digimon API doesn’t rely on the pokemon API response, you could trigger them separately and only
await
on the Promise once you need it.Perhaps I'm misunderstanding your suggestion, but you either need to assign the result to a variable or use the Promises' chain.
e.g.
will result in a TypeError.
So you need to write it as
which defeats the purpose (depending on the use case, the Promise may be intended to be used later)
or
Thanks for clarifying it. That was exactly what I was trying to say, If you don't need it that exact moment you do not have to await it. This is one of pitfalls of async/await.