[Vuejs]-Run a set of code inside asynData function using Nuxt JS

0๐Ÿ‘

โœ…

call function in return method of async data.

asyncData (context) {
    return function1()
}

async function1(){
    await fisrtfunction()
    await secondfunction()
    return true
}

0๐Ÿ‘

If you are using a getter, you can point to a null value that is assigned asynchronously and it will function when updated, but you should put it into computed instead.

computed: {
     return this.$store.getters.getAuthenticatedUser
    }

But hereโ€™s the problem: if you have to await your getter, your design has an issue. For my Vue/Vuex application, I handle login this way:
โ€“ User logs in, and axios makes a call to the back-end to authenticate the user asynchronously (no store interaction)
โ€“ User is authenticated successfully and data comes from the database
โ€“ This data is committed to the store for easy access
โ€“ My getter points to the user data in the store (which is not asynchronous)

I highly recommend storing the user data upon authentication and pointing your getter there.

Leave a comment