Skip to content

fix(reactivity): preserve watch callback return value when wrapped for once: true#14902

Open
ackwell wants to merge 1 commit into
vuejs:mainfrom
ackwell:reactivity-watch-once-async-error-handling
Open

fix(reactivity): preserve watch callback return value when wrapped for once: true#14902
ackwell wants to merge 1 commit into
vuejs:mainfrom
ackwell:reactivity-watch-once-async-error-handling

Conversation

@ackwell
Copy link
Copy Markdown

@ackwell ackwell commented Jun 1, 2026

watch callbacks are executed via callWithAsyncErrorHandling, which inspects the return value of the callback to determine if promise rejection handling is required.

when passing once: true to watch's options, the callback is wrapped to add the watchHandle call after the first execution - in the process, silently dropping any returned promises.

this pr is just a small tweak + test to preserve the original callback's result, returning it from the new wrapped method.

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Fixed watch() function to properly return callback values when the once option is used
    • Improved error handling for async callbacks in watchers to correctly surface errors through the error handler

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 1, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e0b3acc1-79db-4fd9-a620-6d9c1f9c69c2

📥 Commits

Reviewing files that changed from the base of the PR and between 86ad076 and 022d790.

📒 Files selected for processing (2)
  • packages/reactivity/__tests__/watch.spec.ts
  • packages/reactivity/src/watch.ts

📝 Walkthrough

Walkthrough

The PR fixes the watch function to return the original callback's result when once mode is enabled. It adds comprehensive tests validating that async callbacks throw errors properly via the onError handler in both standard and once: true scenarios.

Changes

Watch callback behavior with once mode

Layer / File(s) Summary
Callback return value capture in once mode
packages/reactivity/src/watch.ts, packages/reactivity/__tests__/watch.spec.ts
When once && cb are both enabled, the internal wrapper now stores the callback's result and returns it instead of discarding the value. Tests verify that async callbacks correctly invoke onError when they throw, for both standard and once: true watch modes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

ready to merge, scope: reactivity, :hammer: p3-minor-bug

Poem

A rabbit hops through watch's flow,
Callbacks return what they should show,
Once mode keeps its promise bright,
Async errors see the light,
All caught and handled, smooth and right! 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing watch callback return value preservation when once: true is used, which is the core issue addressed in the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant