I'm referring to section Stopping a Watcher on the Composable page.
It says:
The key here is that the watcher must be created synchronously: if the watcher is created in an async callback, it won't be bound to the owner component and must be stopped manually to avoid memory leaks.
There's then an example where watch is incorrectly called inside a setTimeout callback.
The terms "synchronously" and "async callback" are confusing with respect to top-level await inside a script setup component.
In the current Vue release, it is safe to call watch after await, as the SFC compiler keeps track of current component instance across await boundaries.
This fact is actually called out on page Composables, section Usage restrictions.
Also important: is the watcher still cleaned-up if the component is removed before even being mounted (i.e. before await resolves)?
This SFC Playground shows that this situation is safe as the watcher does not even run.
I'm referring to section Stopping a Watcher on the Composable page.
It says:
There's then an example where
watchis incorrectly called inside asetTimeoutcallback.The terms "synchronously" and "async callback" are confusing with respect to top-level
awaitinside ascript setupcomponent.In the current Vue release, it is safe to call
watchafterawait, as the SFC compiler keeps track of current component instance across await boundaries.This fact is actually called out on page Composables, section Usage restrictions.
Also important: is the watcher still cleaned-up if the component is removed before even being mounted (i.e. before await resolves)?
This SFC Playground shows that this situation is safe as the watcher does not even run.