Hey, starting with koa on back-end, a lot of stuff are moving to async/await (*/yield for now), what I suggest is adding async/await functions support to methods so it's allowed to do like:
import DBService from './db'
new Vue({
el: document.body,
data: {
user: {},
friends: []
},
methods: {
async getUserData () {
this.user = await DBService.getUser()
this.friends = await DBService.getFriends(this.user.id)
}
}
})
This looks like a cleaner way of doing async stuff to me, what do you think?
P.S. Same can be used for Vuex actions.
And you can't?
I didn't test it, but I think you can.
Just remember that methods
is an object, so it should be:
import DBService from './db'
new Vue({
el: document.body,
data: {
user: {},
friends: []
},
methods: {
getUserData: async function() {
this.user = await DBService.getUser()
this.friends = await DBService.getFriends(this.user.id)
}
}
})
Same for actions. It's mentioned here https://github.com/vuejs/vuex/releases/tag/v2.0.0-rc.1
@NicolasParada, sorry, I don't know why but I was thinking that async functions get called only when you call .then on them. My bad.
@NicolasParada, btw, my notation is shortand just like:
var a = {
myFunction () {},
async myAsyncFunction () {}
}
I know, you can't use the short-hand syntax together with async.
var a = {
myFunction () {},
myAsyncFunction: async function () {}
}
@NicolasParada, you can https://babeljs.io/repl/#?evaluate=true&lineWrap=false&presets=es2015%2Creact%2Cstage-2&code=a%20%3D%20%7B%0A%20%20async%20hello%20()%20%7B%0A%20%20%20%20%0A%20%20%7D%0A%7D
If you use babel-preset-stage-3
(or earlier) and babel-plugin-transform-runtime
, it actually works fine. I had some async functions running in my project for a while.
Yeah, you can.
Sorry, my bad. I don't know why I thought that.
https://gist.github.com/NicolasParada/5e55e4c26bc2a40e6700a45408c14513
The one that doesn't is computed.
@NicolasParada, I dont' think it is possible to implement async/await in computed because it's extremely hard to track changes on async functions, you can't predict that function calls after "await" have changed.
You can have async in computed, now. There's a module for it.
Most helpful comment
And you can't?
I didn't test it, but I think you can.
Just remember that
methods
is an object, so it should be:Same for actions. It's mentioned here https://github.com/vuejs/vuex/releases/tag/v2.0.0-rc.1