Skip to content

until

类别
导出体积
609 B
上次更改
6 minutes ago

等待变化的一次性 Promise

示例

将计数增加到 7 以显示警报。

计数: 0

用法

等待一些异步数据准备就绪

ts
import { 
until
,
useAsyncState
} from '@vueuse/core'
const {
state
,
isReady
} =
useAsyncState
(
fetch
('https://jsonplaceholder.typicode.com/todos/1').
then
(
t
=>
t
.
json
()),
{}, ) ;(async () => { await
until
(
isReady
).
toBe
(true)
console
.
log
(
state
) // 状态现在已经准备就绪!
})()

等待自定义条件

你可以使用 invoke 调用异步函数。

ts
import { 
invoke
,
until
,
useCounter
} from '@vueuse/core'
const {
count
} =
useCounter
()
invoke
(async () => {
await
until
(
count
).
toMatch
(
v
=>
v
> 7)
alert
('计数器现在大于 7!')
})

超时

ts
// 将会在 ref.value === true 或者 1000ms 过去时解析
await 
until
(
ref
).
toBe
(true, {
timeout
: 1000 })
// 如果超时将会抛出错误 try { await
until
(
ref
).
toBe
(true, {
timeout
: 1000,
throwOnTimeout
: true })
// ref.value === true } catch (
e
) {
// 超时 }

更多示例

ts
await 
until
(
ref
).
toBe
(true)
await
until
(
ref
).
toMatch
(
v
=>
v
> 10 &&
v
< 100)
await
until
(
ref
).
changed
()
await
until
(
ref
).
changedTimes
(10)
await
until
(
ref
).
toBeTruthy
()
await
until
(
ref
).
toBeNull
()
await
until
(
ref
).
not
.
toBeNull
()
await
until
(
ref
).
not
.
toBeTruthy
()

类型声明

显示类型声明
ts
export interface UntilToMatchOptions {
  /**
   * 当条件未满足时,Promise resolve/reject 的毫秒超时时间。
   * 0 表示永不超时
   *
   * @default 0
   */
  
timeout
?: number
/** * 当超时时是否拒绝 Promise * * @default false */
throwOnTimeout
?: boolean
/** * 内部监视的 `flush` 选项 * * @default 'sync' */
flush
?:
WatchOptionFlush
/** * 内部监视的 `deep` 选项 * * @default 'false' */
deep
?:
WatchOptions
["deep"]
} export interface
UntilBaseInstance
<
T
,
Not
extends boolean = false> {
toMatch
: (<
U
extends
T
=
T
>(
condition
: (
v
:
T
) =>
v
is
U
,
options
?: UntilToMatchOptions,
) =>
Not
extends true ?
Promise
<
Exclude
<
T
,
U
>> :
Promise
<
U
>) &
((
condition
: (
v
:
T
) => boolean,
options
?: UntilToMatchOptions,
) =>
Promise
<
T
>)
changed
: (
options
?: UntilToMatchOptions) =>
Promise
<
T
>
changedTimes
: (
n
?: number,
options
?: UntilToMatchOptions) =>
Promise
<
T
>
} type
Falsy
= false | void | null | undefined | 0 | 0n | ""
export interface
UntilValueInstance
<
T
,
Not
extends boolean = false>
extends
UntilBaseInstance
<
T
,
Not
> {
readonly
not
:
UntilValueInstance
<
T
,
Not
extends true ? false : true>
toBe
: <
P
=
T
>(
value
:
MaybeRefOrGetter
<
P
>,
options
?: UntilToMatchOptions,
) =>
Not
extends true ?
Promise
<
T
> :
Promise
<
P
>
toBeTruthy
: (
options
?: UntilToMatchOptions,
) =>
Not
extends true ?
Promise
<
T
&
Falsy
> :
Promise
<
Exclude
<
T
,
Falsy
>>
toBeNull
: (
options
?: UntilToMatchOptions,
) =>
Not
extends true ?
Promise
<
Exclude
<
T
, null>> :
Promise
<null>
toBeUndefined
: (
options
?: UntilToMatchOptions,
) =>
Not
extends true ?
Promise
<
Exclude
<
T
, undefined>> :
Promise
<undefined>
toBeNaN
: (
options
?: UntilToMatchOptions) =>
Promise
<
T
>
} export interface
UntilArrayInstance
<
T
> extends
UntilBaseInstance
<
T
> {
readonly
not
:
UntilArrayInstance
<
T
>
toContains
: (
value
:
MaybeRefOrGetter
<
ElementOf
<
ShallowUnwrapRef
<
T
>>>,
options
?: UntilToMatchOptions,
) =>
Promise
<
T
>
} /** * 等待变化的一次性 Promise * * @see https://vueuse.org/until * @example * ``` * const { count } = useCounter() * * await until(count).toMatch(v => v > 7) * * alert('计数器现在大于 7!') * ``` */ export declare function
until
<
T
extends unknown[]>(
r
:
WatchSource
<
T
> |
MaybeRefOrGetter
<
T
>,
):
UntilArrayInstance
<
T
>
export declare function
until
<
T
>(
r
:
WatchSource
<
T
> |
MaybeRefOrGetter
<
T
>,
):
UntilValueInstance
<
T
>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Anthony Fu
Arthur Darkstone
SerKo
Robin
IlyaL
James Garbutt
OrbisK
Gianthard-cyh
sun0day
lsdsjy
Mitchell
Jeff Zou
wheat
Shinigami
Alex Kozack

更新日志

7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
8a023 - fix: cleanup at next tick to avoid memory leak (#4039)
a086e - fix: stricter types