Open
Description
<script setup lang="ts">
import { ref, watch } from 'vue'
import type { Ref } from 'vue'
const count = ref(0)
/**
* Implement the until function
*/
function until(initial: Ref<number>) {
function toBe(value: number) {
return new Promise((resolve) => {
const stop = watch(
() => initial.value,
(newVal) => {
if (newVal === value) {
resolve(true)
stop()
}
},
)
})
}
return {
toBe,
}
}
async function increase() {
let timer: number | undefined = undefined
clearInterval(timer)
count.value = 0
timer = setInterval(() => {
count.value++
}, 1000)
await until(count).toBe(3)
clearInterval(timer)
console.log(count.value === 3) // Make sure the output is true
}
</script>
<template>
<p @click="increase">Increase</p>
<p>count: {{ count }}</p>
</template>