Vue: [feature-request] async/await methods in Vue 2.0

Created on 22 Jul 2016  Â·  10Comments  Â·  Source: vuejs/vue

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.

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:

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

All 10 comments

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.

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robertleeplummerjr picture robertleeplummerjr  Â·  3Comments

lmnsg picture lmnsg  Â·  3Comments

bdedardel picture bdedardel  Â·  3Comments

finico picture finico  Â·  3Comments

aviggngyv picture aviggngyv  Â·  3Comments