Skip to content

hmon/async-computed

Repository files navigation

Async Computed

A vue composition api compatible async computed. With this you can have async computed hooks in vue 3+ or vue 2+ with composition api. For example:

<script>
import { computed } from 'vue'
import asyncComputed from '@vue3-composables/async-computed'

export default {
  setup () {
    const userId = ref('foo')

    // This doesn't work
    const promise = computed(() => {
      return callApi('/users/' + userId.value)
        .then(response => response.data.name)
    })

    // This works
    const name = asyncComputed(() => {
      return callApi('/users/' + userId.value)
        .then(response => response.data.name)
    })
  }
}
</script>

Installing

npm install --save @vue3-composables/async-computed