whenever

Shorthand for watching value to be truthy.

Usage

import { whenever, useAsyncState } from '@vueuse/core'

const { state, ready } = useAsyncState(
  fetch('https://jsonplaceholder.typicode.com/todos/1').then(t => t.json()),
  {},
)

whenever(ready, () => console.log(state))
// this
whenever(ready, () => console.log(state))

// is equivalent to:
watch(ready, (isReady) => {
  if (isReady) {
    console.log(state)
  }
})

Computed

Same as watch, you can pass a getter function to calculate on each change.

// this
whenever(
  () => counter.value === 7, 
  () => console.log('counter is 7 now!'),
)

Options

Options and defaults are same with watch.

// this
whenever(
  () => counter.value === 7, 
  () => console.log('counter is 7 now!'),
  { flush: 'sync' }
)

Type Declarations

/**
 * Shorthand for watching value to be truthy
 *
 * @see https://vueuse.js.org/whenever
 */
export declare function whenever<T = boolean>(
  source: WatchSource<T>,
  cb: Fn,
  options?: WatchOptions
): WatchStopHandle

Source

SourceDocs