[Vuejs]-Vue exports.function syntax

0👍

For your question you just need to place an async before your function

exports.getScore = async (storeId) => { /* snip */ }

But actually there are some problem in your code
First, you can pass a promise in Promise executor’s resolve

new Promise(resolve => {
  // This will work
  resolve(anotherPromise)
})

However, if you already have a promise you can just return it directly
If you want to make sure it’s a native Promise or you are not sure that it’s a Promise or not.
Wrap it in Promise.resolve

Promise.resolve(anotherPromiseOrValue)

So your code can change into:

exports.getScore = (storeId) => this.getScore('day', storeId)

And it also equal:

exports.getScore = this.getScore.bind(this, 'day')

But there is another problem: what is the this here ?
If you extract this function from Vue methods, then the this probably is a Vue instance.
Then I afraid that the above code wont work as you expected.
So I recommend that you just use getScore in your component’s lifecycle hook, like mounted

For example:

mounted () {
  this.getScore('day', this.storeId).then(score => {
    this.score = score
  })
}

Or with async/await

async mounted () {
  this.score = await this.getScore('day', this.storeId)
}

Leave a comment