L
L
lacront2018-10-17 13:57:52
Vue.js
lacront, 2018-10-17 13:57:52

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;
    });
  },

Tell me, please, did I do it right?
It is also not clear what to do with return, in fact nothing needs to be returned.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-10-18
@lacront

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 question

Ask a Question

731 491 924 answers to any question