D
D
danilr2019-04-19 12:58:54
Vue.js
danilr, 2019-04-19 12:58:54

How to return a value from a promise in Vuex?

There is such an action:

actions: {
    getAllData(context) {
      console.log(new Date());
      return new Promise((resolve, reject) => {
        HTTP.post(Routes.getResidentialsOnMap, {})
          .then(({ data }) => {

            console.log(new Date());
            console.log('data: ', data);
            context.commit('getAllData', data);
            
            resolve(data);
          })
          .catch(error => {
            console.error(error);
            reject(error);
          });
      })

It is called in created in one of the components, I need to execute the function as soon as it receives the data and performs the necessary mutation in the other component. Question - how to do it?
I know that action returns a promise, but as I understand it, for this you need to call action, and I need to call it only once (since I need its result in two different components, I don’t understand how to do this

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alex, 2019-04-19
@Kozack Vue.js

https://vuex.vuejs.org/guide/actions.html#composin...

N
niksee, 2017-03-05
@niksee

Everything works on my iPhone 5SE

S
Sergey Goryachev, 2017-03-05
@webirus

Is there a script somewhere?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question