Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
VueJS Vuex Action explanation

I'm a beginner in vueJS and I'd like to know something about actions. As far as I know, I've seen that they would handle asynchronous code while mutations hold synchronous one. I'd like to understand something . Let's suppose I call an API when a component / view gets mounted :

mounted(){
          fetch("http://localhost:100/api/v1/tags")
          .then(response =>
            response.json())
          .then(
           data => { 
            this.dbTags = [...data.map(item => item.nom)]
            this.$store.commit('changeTagsApiValueToTrue');
            }
          ) 

I used commit here and it works but I want to know whether it is correct to use it when the code outside of the action is asynchronous. What I mean is, does the code inside the action part of the vuex file should be asynchronous or every single asynchronous functionality that is using vuex (such as fetching an api from a component / view ) should have its data handled by actions.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There is nothing wrong with calling a synchronous method from an asynchronous chain.

You only need to use actions (aka dispatch) when your call into the store uses asynchronous code.

In other words, if your call to commit called fetch, you should use dispatch instead.

store.dispatch('doFetch', 'http://localhost:100/api/v1/tags')

Then your 'doFetch' action would call fetch, and eventually call commit

If your address was always the same, it would simply be:

store.dispatch('doFetch')
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!