Skip to content

Latest commit

 

History

History
157 lines (117 loc) · 3.85 KB

README-zh_CN.md

File metadata and controls

157 lines (117 loc) · 3.85 KB

untiljs

Promise观察一次性变化

NPM version Codacy Badge tree shaking typescript Test coverage npm download gzip License

Sonar

使用其他语言阅读:English | 简体中文

体验

在线体验 untiljs 功能 Edit in CodeSandbox

安装

# 使用pnpm
$ pnpm install untiljs

# 使用npm
$ npm install untiljs --save

# 使用 yarn
$ yarn add untiljs

使用

1. 等待异步数据加载完成

import { ref } from 'vue'
import until from 'untiljs'

const val = ref(1)

setTimeout(() => {
  val.value = 2
}, 1000)
;(async () => {
  await until(val).toBe(2)
  // state is now ready
})()

2. 自定义表达式

import { ref } from 'vue'
import until from 'untiljs'

const val = ref(1)

setTimeout(() => {
  val.value = 2
}, 1000)
;(async () => {
  await until(val).toMatch(value => value > 1)
  // state is now ready
})()

3. 等待直到超时

import { ref } from 'vue'
import until from 'untiljs'

const val = ref(1)

setTimeout(() => {
  val.value = 2
}, 1000)
;(async () => {
  await until(val).not.toBe(ref(2), { timeout: 500, throwOnTimeout: true })
  // reject timeout
})()

4. 更多示例

import { ref } from 'vue'
import until from 'untiljs'

const val = ref(1)

setTimeout(() => {
  val.value = 2
}, 1000)
;(async () => {
  await until(ref).toBe(true)
  await until(ref).toMatch(v => v > 5 && v < 10)
  await until(ref).changed()
  await until(ref).changedTimes(2)
  await until(ref).toBeTruthy()
  await until(ref).toBeNull()

  await until(ref).not.toBeNull()
  await until(ref).not.toBeTruthy()
})()

使用 unpkg CDN

<script src="https://unpkg.com/vue-demi@latest/lib/index.iife.js"></script>
<script src="https://unpkg.com/untiljs@latest/dist/index.min.js"></script>
<script>
  await until(val).toBe(true)
  // ...
</script>

问题和支持

Please open an issue here.

License

MIT