Skip to content

until

Category
Last Changed
1月8日10时40分

Promised one-time watch for changes

Demo

Add to 7 to show the alert.

Count: 0

Usage

Wait for some async data to be ready

js
import { until, useAsyncState } from 'comuse/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) // state is now ready!
})()

Wait for custom conditions

You can use invoke to call the async function.

js
import { invoke, until, useCounter } from 'comuse-core'

const { count } = useCounter()

invoke(async () => {
  await until(count).toMatch(v => v > 7)

  alert('Counter is now larger than 7!')
})

Timeout

ts
// will be resolve until ref.value === true or 1000ms passed
await until(ref).toBe(true, { timeout: 1000 })

// will throw if timeout
try {
  await until(ref).toBe(true, { timeout: 1000, throwOnTimeout: true })
  // ref.value === true
}
catch (e) {
  // timeout
}

More Examples

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()

Source

SourceDemoDocs

Changelog

v3.1.0 on 1/9/2025
59615 - feat: add throttle function
v3.0.0 on 1/7/2025
825ca - feat: shared add functions animation, ease, gesture
30b37 - feat: add test scripts
e3dd2 - feat: add until functions

Released under the MIT License.