question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

41reactions
nicolasparadacommented, Jul 22, 2016

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

10reactions
nickmessingcommented, Jul 22, 2016

@NicolasParada, btw, my notation is shortand just like:

var a = {
  myFunction () {},
  async myAsyncFunction () {}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Async/Await with Vue.js Components
We can simply declare the created () method as async and perform our asynchronous actions inside. In this example, we are loading a...
Read more >
How to Use Async and Await with Vue.js Apps - Medium
Vue.js is a great framework for building front end web apps. ... In the save function of the methods object, we have: async...
Read more >
Async / Await method in Vue.js - Stack Overflow
Try to wait for method too: async getTreeView(){ await fetch("myurl /*just a URL */", { method: 'GET', }) .then((response) ...
Read more >
Data Fetching - Nuxt
In Nuxt we have 2 ways of getting data from an API. We can use the fetch method or the asyncData method. Nuxt...
Read more >
Asynchronous Behavior | Vue Test Utils
You may have noticed some other parts of the guide using await when calling some methods on wrapper , such as trigger and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found