Challenge - 5 Problems
Debounced Watcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when the watched value changes rapidly in this debounced watcher?
Consider a Vue 3 component using a debounced watcher on a reactive property. If the property changes multiple times quickly, what will the watcher callback do?
Vue
<script setup> import { ref, watch } from 'vue' import { debounce } from 'lodash-es' const searchTerm = ref('') const debouncedSearch = debounce((newVal) => { console.log('Search for:', newVal) }, 300) watch(searchTerm, (newVal) => { debouncedSearch(newVal) }) </script>
Attempts:
2 left
💡 Hint
Think about how debounce delays function calls until changes stop.
✗ Incorrect
Debounce delays the execution of the callback until 300ms have passed without new calls. Rapid changes reset the timer, so only the last change triggers the callback after the delay.
📝 Syntax
intermediate2:00remaining
Which option correctly implements a debounced watcher in Vue 3 Composition API?
Select the code snippet that correctly sets up a debounced watcher on a reactive property named 'query' using lodash debounce.
Attempts:
2 left
💡 Hint
Debounce wraps the callback, not the reactive source.
✗ Incorrect
You create a debounced function first, then pass it as the watcher callback. Wrapping the source or calling debounce inside the watcher callback is incorrect.
🔧 Debug
advanced2:00remaining
Why does this debounced watcher log stale values?
This Vue 3 component uses a debounced watcher but logs old values instead of the latest. What causes this behavior?
Vue
<script setup> import { ref, watch } from 'vue' import { debounce } from 'lodash-es' const input = ref('') watch(input, (newVal) => { debounce(() => { console.log(newVal) }, 300)() }) </script>
Attempts:
2 left
💡 Hint
Check when the debounced function reads the reactive value.
✗ Incorrect
The debounced function closes over newVal at creation time, so it logs the value from when it was created. To fix, pass the value as an argument to the debounced function.
❓ state_output
advanced2:00remaining
What is the console output sequence when rapidly updating a debounced watcher?
Given this Vue 3 code, what will be printed to the console if 'text' changes from '' to 'a', then 'ab', then 'abc' within 100ms intervals?
Vue
<script setup> import { ref, watch } from 'vue' import { debounce } from 'lodash-es' const text = ref('') const debouncedPrint = debounce((val) => { console.log('Value:', val) }, 200) watch(text, (val) => { debouncedPrint(val) }) // Simulate rapid changes: text.value = 'a' setTimeout(() => { text.value = 'ab' }, 100) setTimeout(() => { text.value = 'abc' }, 200) </script>
Attempts:
2 left
💡 Hint
Debounce resets the timer on each call within the delay.
✗ Incorrect
Each change resets the debounce timer. Only the last value 'abc' is logged after 200ms from the last change.
🧠 Conceptual
expert2:00remaining
Why prefer debounced watchers over immediate watchers for expensive operations?
In Vue 3, why is using a debounced watcher better than an immediate watcher for operations like API calls triggered by user input?
Attempts:
2 left
💡 Hint
Think about how debounce groups rapid changes into one action.
✗ Incorrect
Debounced watchers wait for a pause in changes before running the operation, preventing excessive calls and improving performance.