Skip to content

Clarify what is an "async callback" in "Stopping a Watcher" #3447

Description

@jods4

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions