fix(core): do not reject promise right away - only when `promise` gets used #9576
main fromrefs/pull/9576/head·merged by TkDodo fixes #8209
Summary by CodeRabbit#
New Features
- None.
Bug Fixes
- Prevented premature errors when the experimental prefetch-in-render flag is disabled; errors now occur only if a query’s promise is explicitly accessed.
Refactor
- Switched from eager to lazy error handling to avoid initialization failures and preserve normal behavior unless the promise is used.
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelspackage: query-core
View your CI Pipeline Execution ↗ for commit 4a51a5452d40e0340fc07dc42ac7cc2362775967
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 47s View ↗
☁️ Nx Cloud last updated this comment at
2025-08-19 09:36:38UTCMore templates
- @tanstack/query-example-angular-auto-refetching
- @tanstack/query-example-angular-basic
- @tanstack/query-example-angular-basic-persister
- @tanstack/query-example-angular-devtools-panel
- @tanstack/query-example-angular-infinite-query-with-max-pages
- @tanstack/query-example-angular-optimistic-updates
- @tanstack/query-example-angular-pagination
- @tanstack/query-example-angular-query-options-from-a-service
- @tanstack/query-example-angular-router
- @tanstack/query-example-angular-rxjs
- @tanstack/query-example-angular-simple
- @tanstack/query-example-react-algolia
- @tanstack/query-example-react-auto-refetching
- @tanstack/query-example-react-basic
- @tanstack/query-example-react-basic-graphql-request
- @tanstack/query-example-chat
- @tanstack/query-example-react-default-query-function
- @tanstack/query-example-react-devtools-panel
- @tanstack/query-example-eslint-legacy
- @tanstack/query-example-react-infinite-query-with-max-pages
- @tanstack/query-example-react-load-more-infinite-scroll
- @tanstack/query-example-react-nextjs
- @tanstack/query-example-react-nextjs-app-prefetching
- @tanstack/query-example-nextjs-suspense-streaming
- @tanstack/query-example-react-offline
- @tanstack/query-example-react-optimistic-updates-cache
- @tanstack/query-example-react-optimistic-updates-ui
- @tanstack/query-example-react-pagination
- @tanstack/query-example-react-playground
- @tanstack/query-example-react-prefetching
- @tanstack/query-example-react-react-native
- @tanstack/query-example-react-router
- @tanstack/query-example-react-rick-morty
- @tanstack/query-example-react-shadow-dom
- @tanstack/query-example-react-simple
- @tanstack/query-example-react-star-wars
- @tanstack/query-example-react-suspense
- @tanstack/query-example-solid-astro
- @tanstack/query-example-solid-basic
- @tanstack/query-example-solid-basic-graphql-request
- @tanstack/query-example-solid-default-query-function
- @tanstack/query-example-solid-simple
- @tanstack/query-example-solid-start-streaming
- @tanstack/query-example-svelte-auto-refetching
- @tanstack/query-example-svelte-basic
- @tanstack/query-example-svelte-load-more-infinite-scroll
- @tanstack/query-example-svelte-optimistic-updates
- @tanstack/query-example-svelte-playground
- @tanstack/query-example-svelte-simple
- @tanstack/query-example-svelte-ssr
- @tanstack/query-example-svelte-star-wars
- @tanstack/query-example-vue-2.6-basic
- @tanstack/query-example-vue-2.7-basic
- @tanstack/query-example-vue-basic
- @tanstack/query-example-vue-dependent-queries
- @tanstack/query-example-vue-nuxt3
- @tanstack/query-example-vue-persister
- @tanstack/query-example-vue-simple
@tanstack/angular-query-devtools-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-devtools-experimental@9576@tanstack/angular-query-experimental
npm i https://pkg.pr.new/@tanstack/angular-query-experimental@9576@tanstack/eslint-plugin-query
npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@9576@tanstack/query-async-storage-persister
npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@9576@tanstack/query-broadcast-client-experimental
npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@9576@tanstack/query-core
npm i https://pkg.pr.new/@tanstack/query-core@9576@tanstack/query-devtools
npm i https://pkg.pr.new/@tanstack/query-devtools@9576@tanstack/query-persist-client-core
npm i https://pkg.pr.new/@tanstack/query-persist-client-core@9576@tanstack/query-sync-storage-persister
npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@9576@tanstack/react-query
npm i https://pkg.pr.new/@tanstack/react-query@9576@tanstack/react-query-devtools
npm i https://pkg.pr.new/@tanstack/react-query-devtools@9576@tanstack/react-query-next-experimental
npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@9576@tanstack/react-query-persist-client
npm i https://pkg.pr.new/@tanstack/react-query-persist-client@9576@tanstack/solid-query
npm i https://pkg.pr.new/@tanstack/solid-query@9576@tanstack/solid-query-devtools
npm i https://pkg.pr.new/@tanstack/solid-query-devtools@9576@tanstack/solid-query-persist-client
npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@9576@tanstack/svelte-query
npm i https://pkg.pr.new/@tanstack/svelte-query@9576@tanstack/svelte-query-devtools
npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@9576@tanstack/svelte-query-persist-client
npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@9576@tanstack/vue-query
npm i https://pkg.pr.new/@tanstack/vue-query@9576@tanstack/vue-query-devtools
npm i https://pkg.pr.new/@tanstack/vue-query-devtools@9576commit:
4a51a54Codecov Report#
:x: Patch coverage is
40.00000%with3 linesin your changes missing coverage. Please review. :white_check_mark: Project coverage is 59.23%. Comparing base (e4f5ce8) to head (4a51a54). :warning: Report is 2 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #9576 +/- ## =========================================== + Coverage 45.16% 59.23% +14.06% =========================================== Files 208 137 -71 Lines 8320 5561 -2759 Branches 1873 1499 -374 =========================================== - Hits 3758 3294 -464 + Misses 4115 1963 -2152 + Partials 447 304 -143- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between f1e608bcac59a309f61057faa951bd0fad8f75a1 and d613c2a2049148dac02309208d854c8c663d6c9c.
📒 Files selected for processing (1)
packages/query-core/src/queryObserver.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/query-core/src/queryObserver.ts (1)
packages/query-core/src/thenable.ts (1)
PendingThenable(37-37)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Preview
- GitHub Check: Test
packages/query-core/src/queryObserver.ts lines 270 to 278Outdated@@ -272,3 +267,12 @@272267 get: (target, key) => {273268 this.trackProp(key as keyof QueryObserverResult)274269 onPropTracked?.(key as keyof QueryObserverResult)270+ console.log('tracking prop', key)271+ if (key === 'promise' && !this.options.experimental_prefetchInRender) {272+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition273+ ;(this.#currentThenable as PendingThenable<TData>).reject?.(274+ new Error(275+ 'experimental_prefetchInRender feature flag is not enabled',276+ ),277+ )278+ }⚠️ Potential issue
Remove debug log; guard rejection to pending thenables; type-safe key check
- console.log in a hot Proxy.get path will spam user consoles and degrade performance. Remove before merging.
- Only reject if the thenable is still pending to avoid repeated rejections on subsequent accesses.
- Be explicit about key type to avoid symbol keys triggering unintended behavior.
Apply:
- console.log('tracking prop', key) - if (key === 'promise' && !this.options.experimental_prefetchInRender) { - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - ;(this.#currentThenable as PendingThenable<TData>).reject?.( - new Error( - 'experimental_prefetchInRender feature flag is not enabled', - ), - ) - } + if ( + typeof key === 'string' && + key === 'promise' && + !this.options.experimental_prefetchInRender + ) { + const thenable = this.#currentThenable as PendingThenable<TData> + if (thenable.status === 'pending') { + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + thenable.reject?.( + new Error( + 'experimental_prefetchInRender feature flag is not enabled', + ), + ) + } + }If helpful, I can add unit tests to assert:
- No rejection occurs when result.promise is not accessed.
- Rejection occurs exactly once when accessed with the flag disabled.
- No console output is produced during normal operation.
📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if ( typeof key === 'string' && key === 'promise' && !this.options.experimental_prefetchInRender ) { const thenable = this.#currentThenable as PendingThenable<TData> if (thenable.status === 'pending') { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition thenable.reject?.( new Error( 'experimental_prefetchInRender feature flag is not enabled', ), ) } }🤖 Prompt for AI Agents
In packages/query-core/src/queryObserver.ts around lines 270 to 278, remove the console.log('tracking prop') and replace the current key check with a type-safe guard (e.g. ensure typeof key === 'string' && key === 'promise') so symbol or non-string keys are ignored; before calling reject, verify the thenable is still pending (guard against settled thenables) so rejection happens at most once and only for PendingThenable instances; use optional chaining/type narrowing to call reject only when the thenable is the pending variant.TkDodo reviewed
OutdatedResolvedby TkDodo@@ -272,3 +267,4 @@272267 get: (target, key) => {273268 this.trackProp(key as keyof QueryObserverResult)274269 onPropTracked?.(key as keyof QueryObserverResult)270+ console.log('tracking prop', key)TkDodo reviewed
OutdatedResolvedby TkDodo@@ -272,3 +267,10 @@272267 get: (target, key) => {273268 this.trackProp(key as keyof QueryObserverResult)274269 onPropTracked?.(key as keyof QueryObserverResult)270+ if (key === 'promise' && !this.options.experimental_prefetchInRender) {271+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition272+ ;(this.#currentThenable as PendingThenable<TData>).reject?.(273+ new Error(274+ 'experimental_prefetchInRender feature flag is not enabled',275+ ),276+ )@KATT the type assertion + optional chaining is necessary because we might not have a
PendingThenablehere and the genericThenabledoesn’t have arejectmethod :/I don’t fully understand why that is but I’m still shipping this fix 😂
right, I need a status check I think:
4a51a545
right, I need a status check I think:
4a51a545
TkDodo merged thisabab082
TkDodo closed this
Sign in to join this conversation.
Walkthrough#
The change defers rejection of the observer's initial thenable until the tracked result's
promiseproperty is accessed. Withexperimental_prefetchInRenderdisabled the thenable is created pending and only rejected lazily onpromiseaccess; behavior unchanged when the flag is enabled.Changes#
packages/query-core/src/queryObserver.ts#currentThenableas pending whenexperimental_prefetchInRenderis disabled; remove eager rejection in constructor; add a Proxy getter that rejects the pending thenable lazily when thepromiseproperty is accessed (uses optional chaining forreject). No public API changes.Sequence Diagram(s)#
Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Assessment against linked issues#
Poem#
Tip
🔌 Remote MCP (Model Context Protocol) integration is now available!
Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.
📜 Recent review details
Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro
💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration.
📥 Commits
Reviewing files that changed from the base of the PR and between b507746b04ec93ebefddce1834bd7c2ded807cc8 and 4a51a5452d40e0340fc07dc42ac7cc2362775967.
📒 Files selected for processing (1)
packages/query-core/src/queryObserver.ts(1 hunks)⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
🔇 Additional comments (2)
✨ Finishing Touches
🧪 Generate unit tests
🪧 Tips
Chat#
There are 3 ways to chat with CodeRabbit:
I pushed a fix in commit <commit_id>, please review it.Open a follow-up GitHub issue for this discussion.@coderabbitaiin a new review comment at the desired location with your query.@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.Support#
Need help? Create a ticket on our support page for assistance with any issues or questions.
CodeRabbit Commands (Invoked using PR/Issue comments)#
Type
@coderabbitai helpto get the list of available commands.Other keywords and placeholders#
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed.@coderabbitai summaryto generate the high-level summary at a specific location in the PR description.@coderabbitaianywhere in the PR title to generate the title automatically.CodeRabbit Configuration File (
.coderabbit.yaml)#.coderabbit.yamlfile to the root of your repository.# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.jsonStatus, Documentation and Community#