I'm introducing myself to Vue + Vuex and I'm having a hard time figuring out where to initialize an API that has an async initialization method.
I obviously can't await
outside an async function like this:
import { RequestSystemProxy } from "../../assets/scripts/RequestSystemProxy"
const system = new RequestSystemProxy();
const handle = await system.Initialize();
const state = { ... };
const getters = { ... };
const actions = { ... methods that use 'handle' ... };
const mutations = { ... };
export default {
state,
getters,
actions,
mutations
}
and Vue doesn't like it when I export a promise like this:
import { RequestSystemProxy } from "../../assets/scripts/RequestSystemProxy"
export default (async function () {
const system = new RequestSystemProxy();
const handle = await system.Initialize();
const state = { ... };
const getters = { ... };
const actions = { ... methods that use 'handle' ... };
const mutations = { ... };
return {
state,
getters,
actions,
mutations
}
})();
My intention is to use handle
inside actions
to make various async requests but that obviously can't happen until the handle
has resolved.
I'm at a bit of a loss here, I'm not sure where the best place to put this is. I suppose I could initialize handle
globally, before I initialize the Vue app, but that puts the initialization, structurally, pretty far away from where it's used. Is there a typical approach for doing something like this?
Any help would be really appreciated!