-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to return promise from an action? #8
Comments
how did you solve this problem??? |
I've been wasting a bunch of time trying to figure out why I can't get this working. I guess this library isn't really in a usable state. |
In my case,
// promise-action.js
import promiseIpc from 'electron-promise-ipc' // yarn add electron-promise-ipc
const DISPATCH = 'promise-action-dispatch'
export default (options = {}) => store => {
function renderer () {
store.dispatchPromise = (type, payload) =>
promiseIpc.send(DISPATCH, {
type,
payload
})
}
function main (store) {
promiseIpc.on(DISPATCH, ({ type, payload }) => {
return store.dispatch(type, payload)
})
}
return process.type === 'renderer'
? renderer()
: main(store)
}
import Vue from 'vue'
import Vuex from 'vuex'
import { createPersistedState, createSharedMutations } from 'vuex-electron'
import createPromiseAction from './promise-action' // <-- ADD
import modules from './modules'
Vue.use(Vuex)
export default new Vuex.Store({
modules,
plugins: [
createPersistedState(),
createSharedMutations(),
createPromiseAction() // <-- ADD
],
strict: process.env.NODE_ENV !== 'production'
})
// Action
const actions = {
yourAction ({commit}, items) {
return 'OK'
}
}
// Component
const response = await this.$store.dispatchPromise('yourAction')
console.log(response) // OK |
Thanks for this plugin. I tried it with axios and it works but i get an error message in the renderer console: const actions = {
login ({commit}, data) {
return axios.post(getTokenUrl, data)
.then(r => {
commit('SET_TOKEN', data)
})
},
} Any idea how to fix this? |
@cwirz probably, Your sending request failed (url, data or method is invalid). axios/axios#972 const actions = {
login ({commit}, data) {
return axios.post(getTokenUrl, data)
.then(r => {
commit('SET_TOKEN', data)
})
.catch(e => {
// when request failed
console.log(e)
})
},
} |
@ktmouk yeah this kinda worked but i wanted to catch the error in the component from where i dispatch the action: this.$store.dispatchPromise('auth/login', this.credentials)
.then(r => {
this.skip()
})
.catch(e => {
this.error = e.response.data
}) This time i dont get any error like that anymore so thanks! |
Was looking all over the internet looking why my Promise handling wasn't working.. Would be great to have a fix in the package itself without having to manually create the plugin. |
In my case I'm usign axios and I return the Promise ( okm.$ws.auth.login really is a method what returns the axios post ):
And from the component I'm using in this way ( you can consume the response from the action and the error from the component too ):
|
@ktmouk thank you so much for your solution. I was searching why I cant get the async replies using dispatch. If you can please add it as a plugin to npm, so others can easily use this. thanks again :) |
The heart of the issue is that there's no way for the result of an action to be passed back to the renderer process. ferm10n@ec3bab4#diff-5ea1fc93a80776ff21fc637ff5967a710b3effa4720112d9b09617662fb2f8afR46-R107
|
In all my projects with vuex I used to call api methods in module's actions, store result in state and then handle result in component method by returning Promise like this:
Component:
Vuex module:
It seems like using
vuex-electron
andcreateSharedMutations
I can't do this anymore, since vuex actions now returnundefined
.So how to handle when action was accomplished?
The text was updated successfully, but these errors were encountered: