0
0
Vueframework~20 mins

Debounced watchers pattern in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Debounced Watcher Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>
AThe watcher callback runs only once, 300ms after the last change stops.
BThe watcher callback runs immediately on every change without delay.
CThe watcher callback runs multiple times, each delayed by 300ms from the previous.
DThe watcher callback never runs because debounce blocks it.
Attempts:
2 left
💡 Hint
Think about how debounce delays function calls until changes stop.
📝 Syntax
intermediate
2: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.
Aconst debounced = debounce((val) => console.log(val), 200); watch(query, debounced)
Bwatch(query, (val) => { debounce(() => console.log(val), 200)() })
Cwatch(query, debounce((val) => { console.log(val) }, 200))
Dwatch(debounce(query, 200), (val) => console.log(val))
Attempts:
2 left
💡 Hint
Debounce wraps the callback, not the reactive source.
🔧 Debug
advanced
2: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>
ADebounce delays the watcher callback, so it logs the value before changes.
BThe debounced function captures the input value only once when created, not on each call.
CThe watcher is not triggered because input is not reactive.
DThe debounce delay is too short to capture the latest value.
Attempts:
2 left
💡 Hint
Check when the debounced function reads the reactive value.
state_output
advanced
2: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>
ANo output because debounce blocks all calls
B
Value: a
Value: ab
Value: abc
CValue: a
DValue: abc
Attempts:
2 left
💡 Hint
Debounce resets the timer on each call within the delay.
🧠 Conceptual
expert
2: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?
AImmediate watchers delay the operation, causing slower UI updates.
BDebounced watchers run the operation immediately on every change, improving responsiveness.
CDebounced watchers reduce the number of times the expensive operation runs by waiting for input to settle.
DImmediate watchers always run only once, so they are less efficient.
Attempts:
2 left
💡 Hint
Think about how debounce groups rapid changes into one action.