Vuex: Use deferred promises in store

52 views Asked by At

I have multiple Vuex modules which should access a single value (feature flag bool) which comes from an API. I want this API value to be loaded lazily, meaning just when the first action inside of another store module needs it. I imagine something like this:

export const default {
   actions: {
      async method1({rootState, commit}){
         // if its the first call to deferred, the value should be loaded through the API
         // otherwise the stored value should be used
         const lazyLoadedValue = await rootState.sharedModule.deferred;
         // do something with lazyLoadedValue
      }
   }
}

I probably could solve it using something like Thenable's but it doesn't seem to fit Vuex style of state, actions and mutations.

What would be the proper way for Vuex?

0

There are 0 answers