--- title: Watchers Created Asynchronously Must Be Manually Stopped impact: HIGH impactDescription: Async-created watchers are not bound to component lifecycle and cause memory leaks type: capability tags: [vue3, watch, watchers, async, memory-leak, lifecycle, cleanup] --- # Watchers Created Asynchronously Must Be Manually Stopped **Impact: HIGH** - Watchers created inside async callbacks (setTimeout, Promise.then, async/await) are not automatically bound to the component instance. They continue running after the component unmounts, causing memory leaks and errors. Always manually stop watchers that are created asynchronously, or restructure your code to create watchers synchronously with conditional logic. ## Task Checklist - [ ] Create watchers synchronously in setup() or lifecycle hooks when possible - [ ] If async creation is unavoidable, store and call the unwatch function - [ ] Use `onUnmounted` to clean up async-created watchers - [ ] Consider using conditional logic inside a sync watcher instead - [ ] Watch for this pattern in setTimeout, Promise callbacks, and after await **Incorrect:** ```vue ``` **Correct:** ```vue ``` ## Preferred Pattern: Conditional Watch Logic ```vue ``` ## Using watchEffect with Conditional Logic ```vue ``` ## Tracking Multiple Async Watchers ```vue ``` ## Reference - [Vue.js Watchers - Stopping a Watcher](https://vuejs.org/guide/essentials/watchers.html#stopping-a-watcher)