Answer the question
In order to leave comments, you need to log in
Is my logic regarding async/await correct?
The essence of the situation is that it is necessary to receive three responses from two different APIs as follows.
('getMarketData') - refers to one API.
('getCoins'), ('getQuote', 'BTC') to another (they need to be executed sequentially, this is mandatory).
Wrote this code:
created(){
this.$store.state.isLoading = true;
const $this = this;
const loadData = async function(){
const market = $this.$store.dispatch('getMarketData');
let coins = await $this.$store.dispatch('getCoins');
coins = await $this.$store.dispatch('getQuote', 'BTC');
return await market + coins;
}
loadData().then(() => {
this.$store.state.isLoading = false;
});
},
Answer the question
In order to leave comments, you need to log in
I don’t see any special logic here - just an unmotivated complication of the code. If you say that "nothing needs to be returned" - why then get the results, add them up? Yes, and grinding one of them, without using it in any way. So: call the first action - leave it unchanged; the second and third are simply awaits, we remove the coins variable; at the end - just await the market, return and remove the summation.
Why store the context in a separate variable? - when creating arrow functions, you can also use async.
Setting isLoading value - why are you changing store state outside of mutation? That's not how things are done.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question