网站开发哪种语言sem培训班
有些时候,我们需要依赖于异步的返回结果做一些后续处理,until
函数在这种场景下非常有用,你能实现它吗 ? 让我们来试试吧 👇:
<script setup lang='ts'>
import { ref,watch } from "vue"const count = ref(0)/*** Implement the until function
*/function until(initial) {function toBe(value) {return new Promise((resolve,reject)=>{const stop = watch(initial,(val)=>{if(val === value){ resolve()stop()}})})}return {toBe,}
}async function increase() {count.value = 0setInterval(() => {count.value++console.log(count.value) }, 1000)await until(count).toBe(3)console.log(count.value === 3) // Make sure the output is true
}</script><template><p @click="increase">Increase22</p>
</template>