import { isRef , onServerPrefetch } from '@vue/composition-api' ; import type { Ref } from '@vue/composition-api' ; import { globalNuxt } from '@nuxtjs/composition-api/dist/runtime/globals' ; import { ssrRef } from './ssr-ref' ; import { validateKey } from './utils' ; param cb param key example export const useAsync = < T > ( cb , key ? ) : Ref < null | T > => { validateKey ( key ) ; const _ref = isRef ( key ) ? key : ssrRef < T | null > ( null , key ) ; if ( ! _ref . value || ( process . env . NODE_ENV === 'development' && process . client && window [ globalNuxt ] ?. context . isHMR ) ) { const p = Promise . resolve ( cb ( ) ) ; if ( process . server ) { onServerPrefetch ( async ( ) => { _ref . value = await p ; } ) ; } else { p . then ( res => ( _ref . value = res ) ) ; } } return _ref as Ref < null | T > ; } ;