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/until.md
1---2category: Watch3---45# until67Promised one-time watch for changes89## Usage1011#### Wait for some async data to be ready1213```ts14import { until, useAsyncState } from '@vueuse/core'1516const { state, isReady } = useAsyncState(17fetch('https://jsonplaceholder.typicode.com/todos/1').then(t => t.json()),18{},19)2021;(async () => {22await until(isReady).toBe(true)2324console.log(state) // state is now ready!25})()26```2728#### Wait for custom conditions2930> You can use `invoke` to call the async function.3132```ts33import { invoke, until, useCounter } from '@vueuse/core'3435const { count } = useCounter()3637invoke(async () => {38await until(count).toMatch(v => v > 7)3940alert('Counter is now larger than 7!')41})42```4344#### Timeout4546```ts47import { until } from '@vueuse/core'48// ---cut---49// will be resolve until ref.value === true or 1000ms passed50await until(ref).toBe(true, { timeout: 1000 })5152// will throw if timeout53try {54await until(ref).toBe(true, { timeout: 1000, throwOnTimeout: true })55// ref.value === true56}57catch (e) {58// timeout59}60```6162#### More Examples6364```ts65import { until } from '@vueuse/core'66// ---cut---67await until(ref).toBe(true)68await until(ref).toMatch(v => v > 10 && v < 100)69await until(ref).changed()70await until(ref).changedTimes(10)71await until(ref).toBeTruthy()72await until(ref).toBeNull()7374await until(ref).not.toBeNull()75await until(ref).not.toBeTruthy()76```7778## Type Declarations7980```ts81export interface UntilToMatchOptions extends ConfigurableFlushSync {82/**83* Milliseconds timeout for promise to resolve/reject if the when condition does not meet.84* 0 for never timed out85*86* @default 087*/88timeout?: number89/**90* Reject the promise when timeout91*92* @default false93*/94throwOnTimeout?: boolean95/**96* `deep` option for internal watch97*98* @default 'false'99*/100deep?: WatchOptions["deep"]101}102export interface UntilBaseInstance<T, Not extends boolean = false> {103toMatch: (<U extends T = T>(104condition: (v: T) => v is U,105options?: UntilToMatchOptions,106) => Not extends true ? Promise<Exclude<T, U>> : Promise<U>) &107((108condition: (v: T) => boolean,109options?: UntilToMatchOptions,110) => Promise<T>)111changed: (options?: UntilToMatchOptions) => Promise<T>112changedTimes: (n?: number, options?: UntilToMatchOptions) => Promise<T>113}114type Falsy = false | void | null | undefined | 0 | 0n | ""115export interface UntilValueInstance<116T,117Not extends boolean = false,118> extends UntilBaseInstance<T, Not> {119readonly not: UntilValueInstance<T, Not extends true ? false : true>120toBe: <P = T>(121value: MaybeRefOrGetter<P>,122options?: UntilToMatchOptions,123) => Not extends true ? Promise<T> : Promise<P>124toBeTruthy: (125options?: UntilToMatchOptions,126) => Not extends true ? Promise<T & Falsy> : Promise<Exclude<T, Falsy>>127toBeNull: (128options?: UntilToMatchOptions,129) => Not extends true ? Promise<Exclude<T, null>> : Promise<null>130toBeUndefined: (131options?: UntilToMatchOptions,132) => Not extends true ? Promise<Exclude<T, undefined>> : Promise<undefined>133toBeNaN: (options?: UntilToMatchOptions) => Promise<T>134}135export interface UntilArrayInstance<T> extends UntilBaseInstance<T> {136readonly not: UntilArrayInstance<T>137toContains: (138value: MaybeRefOrGetter<ElementOf<ShallowUnwrapRef<T>>>,139options?: UntilToMatchOptions,140) => Promise<T>141}142/**143* Promised one-time watch for changes144*145* @see https://vueuse.org/until146* @example147* ```148* const { count } = useCounter()149*150* await until(count).toMatch(v => v > 7)151*152* alert('Counter is now larger than 7!')153* ```154*/155export declare function until<T extends unknown[]>(156r: WatchSource<T> | MaybeRefOrGetter<T>,157): UntilArrayInstance<T>158export declare function until<T>(159r: WatchSource<T> | MaybeRefOrGetter<T>,160): UntilValueInstance<T>161```162