Question presented to candidate:
"After I push an item to a list in a click handler, list.children.length still shows the old count. What is going on, what does nextTick do about it, and where exactly does a nextTick callback run compared with a Promise.then or a setTimeout?"
What a strong answer should cover:
- Vue batches DOM updates: a reactive write queues a job, and the scheduler flushes the queue in a microtask after the current synchronous code.
nextTick()returns the promise for that flush;await nextTick()(or a callback) runs after the DOM has been patched.- Typical uses: measuring, scrolling or focusing an element right after a state change, and tests (Vue Test Utils'
trigger()returnsnextTick()). - Ordering detail: a
nextTickregistered before any write in that tick does not wait for a later flush, andPromise.thencallbacks queued after the write can run before a laternextTickcallback.setTimeout(fn, 0)runs after all of them. - Alternatives:
flush: 'post'watchers,watchPostEffectandonUpdatedfor reacting to every update. - In the Options API it is
this.$nextTick(), withthisbound to the instance.
Clarifying questions expected:
- "Is the goal to read the DOM once after a specific change, or to react after every update?"
- "Is this in a component or in a test?"
Code / implementation expected: Yes β a handler that logs the DOM before and after await nextTick().