Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useStorageAsync.md
1---2category: State3---45# useStorageAsync67Reactive Storage in with async support.89## Usage1011The basic usage please refer to `useStorage`.1213## Wait First Loaded1415When user entering your app, `useStorageAsync()` will start loading value from an async storage,16sometimes you may get the default initial value, not the real value stored in storage at the very17beginning.1819```ts20import { useStorageAsync } from '@vueuse/core'2122const accessToken = useStorageAsync('access.token', '', SomeAsyncStorage)2324// accessToken.value may be empty before the async storage is ready25console.log(accessToken.value) // ""2627setTimeout(() => {28// After some time, the async storage is ready29console.log(accessToken.value) // "the real value stored in storage"30}, 500)31```3233In this case, you can wait the storage prepared, the returned value is also a `Promise`,34so you can wait it resolved in your template or script.3536```ts37// Use top-level await if your environment supports it38const accessToken = await useStorageAsync('access.token', '', SomeAsyncStorage)3940console.log(accessToken.value) // "the real value stored in storage"41```4243If you must wait multiple storages, put them into a `Promise.allSettled()`4445```ts46router.onReady(async () => {47await Promise.allSettled([48accessToken,49refreshToken,50userData,51])5253app.mount('app')54})55```5657There is a callback named `onReady` in options:5859```ts60import { useStorageAsync } from '@vueuse/core'6162// Use ES2024 Promise.withResolvers, you may use any Deferred object or EventBus to do same thing.63const { promise, resolve } = Promise.withResolvers()6465const accessToken = useStorageAsync('access.token', '', SomeAsyncStorage, {66onReady(value) {67resolve(value)68}69})7071// At main.ts72router.onReady(async () => {73// Let's wait accessToken loaded74await promise7576// Now accessToken has loaded, we can safely mount our app7778app.mount('app')79})80```8182Simply use `resolve` as callback:8384```ts85const accessToken = useStorageAsync('access.token', '', SomeAsyncStorage, {86onReady: resolve87})88```8990## Type Declarations9192```ts93export interface UseStorageAsyncOptions<T> extends Omit<94UseStorageOptions<T>,95"serializer"96> {97/**98* Custom data serialization99*/100serializer?: SerializerAsync<T>101/**102* On first value loaded hook.103*/104onReady?: (value: T) => void105}106export declare function useStorageAsync(107key: string,108initialValue: MaybeRefOrGetter<string>,109storage?: StorageLikeAsync,110options?: UseStorageAsyncOptions<string>,111): RemovableRef<string> & Promise<RemovableRef<string>>112export declare function useStorageAsync(113key: string,114initialValue: MaybeRefOrGetter<boolean>,115storage?: StorageLikeAsync,116options?: UseStorageAsyncOptions<boolean>,117): RemovableRef<boolean> & Promise<RemovableRef<boolean>>118export declare function useStorageAsync(119key: string,120initialValue: MaybeRefOrGetter<number>,121storage?: StorageLikeAsync,122options?: UseStorageAsyncOptions<number>,123): RemovableRef<number> & Promise<RemovableRef<number>>124export declare function useStorageAsync<T>(125key: string,126initialValue: MaybeRefOrGetter<T>,127storage?: StorageLikeAsync,128options?: UseStorageAsyncOptions<T>,129): RemovableRef<T> & Promise<RemovableRef<T>>130export declare function useStorageAsync<T = unknown>(131key: string,132initialValue: MaybeRefOrGetter<null>,133storage?: StorageLikeAsync,134options?: UseStorageAsyncOptions<T>,135): RemovableRef<T> & Promise<RemovableRef<T>>136```137