link2329 link2330 link2331 link2332 link2333 link2334 link2335 link2336 link2337 link2338 link2339 link2340 link2341 link2342 link2343 link2344 link2345 link2346 link2347 link2348 link2349 link2350 link2351 link2352 link2353 link2354 link2355 link2356 link2357 link2358 link2359 link2360 link2361 link2362 link2363 link2364 link2365 link2366 link2367 link2368 link2369 link2370 link2371 link2372 link2373 link2374 link2375 link2376 link2377 link2378 link2379 link2380 link2381 link2382 link2383 link2384 link2385 link2386 link2387 link2388 link2389 link2390 link2391 link2392 link2393 link2394 link2395 link2396 link2397 link2398 link2399 link2400 link2401 link2402 link2403 link2404 link2405 link2406 link2407 link2408 link2409 link2410 link2411 link2412 link2413 link2414 link2415 link2416 link2417 link2418 link2419 link2420 link2421 link2422 link2423 link2424 link2425 link2426 link2427 link2428 link2429 link2430 link2431 link2432 link2433 link2434 link2435 link2436 link2437 link2438 link2439 link2440 link2441 link2442 link2443 link2444 link2445 link2446 link2447 link2448 link2449 link2450 link2451 link2452 link2453 link2454 link2455 link2456 link2457 link2458 link2459 link2460 link2461 link2462 link2463 link2464 link2465

[Vue.js] Async / Await Vuex

to call an action in created hook, wait until is done and in same hook to display the result. Is that possible?

I tried to put async / await in actions but doesn’t help.
This is the action property with the async function in the store:

actions: {
async FETCH_USER({commit}) {
await firebase.firestore().collection(‘test’).get().then(res => {
commit(‘FETCH_USER’, res.docs[0].data())
})
}
}

created() {
this.FETCH_USER()
console.log(this.GET_USER)
},
methods: {
…mapActions([
‘FETCH_USER’
]),
login() {
if(this.$refs.form.validate()) {
console.log(‘welcome’)
}
}
},
computed: {
…mapGetters([
‘GET_USER’
])
}

export default new Vuex.Store({
state: {
user: null
},
getters: {
GET_USER: state => state.user
},
mutations: {
FETCH_USER(state, user) {
state.user = user
}
},
actions: {
FETCH_USER({commit}) {
firebase.firestore().collection(‘test’).get().then(res => {
commit(‘FETCH_USER’, res.docs[0].data())
})
}
}
})

Solution :

async/await version

async FETCH_USER({ commit }) {
const res = await firebase.firestore().collection(‘test’).get()
const user = res.docs[0].data()
commit(‘FETCH_USER’, user)
return user
}

async created() {
// The action returns the user out of convenience
const user = await this.FETCH_USER()
console.log(user)

// — or –

// Access the user through the getter
await this.FETCH_USER()
console.log(this.GET_USER)
}

You need to await the action call because it is an async function.

Promise version

FETCH_USER({ commit }) {
return firebase.firestore().collection(‘test’).get().then(res => {
const user = res.docs[0].data()
commit(‘FETCH_USER’, user)
return user
})
}

created() {
this.FETCH_USER().then(user => {
console.log(user)
})

// — or –

this.FETCH_USER().then(() => {
console.log(this.GET_USER)
})
}