Skip to content

whenever

Category
Export Size
173 B
Last Changed
last month

Shorthand for watching value to be truthy.

Usage

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

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

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

Callback Function

Same as watch, the callback will be called with cb(value, oldValue, onInvalidate).

ts
whenever
(height, (
current
,
lastHeight
) => {
if (
current
>
lastHeight
)
console
.
log
(`Increasing height by ${
current
-
lastHeight
}`)
})

Computed

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

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

Options

Options and defaults are same with watch.

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

Type Declarations

ts
type 
Truthy
<
T
> =
T
extends false | null | undefined ? never :
T
export interface
WheneverOptions
<
Immediate
= boolean,
> extends WatchOptions<
Immediate
> {
/** * Only trigger once when the condition is met * * Override the `once` option in `WatchOptions` * * @default false */
once
?: boolean
} /** * Shorthand for watching value to be truthy * * @see https://vueuse.org/whenever */ export declare function
whenever
<
T
>(
source
:
WatchSource
<
T
>,
cb
:
WatchCallback
<
Truthy
<
T
>,
T
| undefined>,
options
?:
WheneverOptions
<true>,
):
WatchHandle
export declare function
whenever
<
T
>(
source
:
WatchSource
<
T
>,
cb
:
WatchCallback
<
Truthy
<
T
>,
T
>,
options
?:
WheneverOptions
<false>,
):
WatchHandle

Source

SourceDocs

Contributors

Anthony Fu
Anthony Fu
Vadim
SerKo
James Garbutt
Chizuki
freakzlike
donotloveshampo
Chung, Lian
Alex Kozack

Changelog

Pending for release...
979c6 - fix: improve old value types (#5096)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.9.0 on
bd946 - feat: override once option (#3800)

Released under the MIT License.

The new era of AI engineering.
Workflows, artifacts, and judgment that scale.
Request Early Access