vue-async-computed is a lightweight plugin for Vue 3 that seamlessly integrates asynchronous operations into Vue's reactive system through a dedicated `asyncComputed` option. It allows developers to define computed properties that return Promises, effectively enabling the use of `async/await` syntax for computations that depend on asynchronous data fetching or complex, time-consuming operations. The current stable version is 4.0.1. This library addresses the inherent limitation of standard `computed` properties, which are synchronous, by providing a declarative way to manage asynchronous state, including automatic re-computation when dependencies change and handling the initial `null` state until the promise resolves. Its primary differentiator is the direct integration into the Vue instance/component options, offering a cleaner syntax compared to manual `watch` or `mounted` based async data handling.
npm install vue-async-computedVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to install `vue-async-computed` and define an asynchronous computed property (`username`) that fetches data from an API based on a reactive `userId`. It showcases the loading state and automatic re-computation.
Ensure that any logic depending on the initial default value of an async computed property is placed within or after the `created` lifecycle hook. Avoid accessing it in `data` or `setup` if an immediate non-null value is expected.
Use conditional rendering (`v-if`, `v-else`) or provide fallback text (`{{ myAsyncValue === null ? 'Loading...' : myAsyncValue }}`) in your templates. Implement null checks in your JavaScript logic before operating on the value.Always use the `asyncComputed` option provided by this plugin for properties that perform asynchronous operations or return Promises. Reserve the `computed` option for synchronous, immediate calculations.
Move the `async` function or Promise-returning function from the `computed` option to the `asyncComputed` option.
Add null checks (`v-if="myAsyncProp"` in template, or `if (this.myAsyncProp) { ... }` in script) before attempting to use the value of an async computed property.