Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/react-query/src/suspense.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as React from 'react'
import type {
DefaultError,
DefaultedQueryObserverOptions,
Expand All @@ -8,6 +9,19 @@ import type {
} from '@tanstack/query-core'
import type { QueryErrorResetBoundaryValue } from './QueryErrorResetBoundary'

/**
* TODO (v6): Bump peer dependency to React 19 and use `React.use` directly.
*/
export function suspend<T>(promise: Promise<T>): void {
if (typeof React.use === 'function') {
// `React.use` can be called conditionally
// eslint-disable-next-line react-hooks/rules-of-hooks
React.use(promise)
} else {
throw promise
}
}

export const defaultThrowOnError = <
TQueryFnData = unknown,
TError = DefaultError,
Expand Down
8 changes: 7 additions & 1 deletion packages/react-query/src/useBaseQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ensureSuspenseTimers,
fetchOptimistic,
shouldSuspend,
suspend,
willFetch,
} from './suspense'
import type {
Expand Down Expand Up @@ -125,7 +126,12 @@ export function useBaseQuery<

// Handle suspense
if (shouldSuspend(defaultedOptions, result)) {
throw fetchOptimistic(defaultedOptions, observer, errorResetBoundary)
const promise = fetchOptimistic(
defaultedOptions,
observer,
errorResetBoundary,
)
suspend(promise.finally(() => observer.updateResult()))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why isn’t this just suspend(promise) ? also, do we need to return after that ? I think throw usually stops execution of the rest?

}

// Handle error boundary
Expand Down
3 changes: 2 additions & 1 deletion packages/react-query/src/useQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ensureSuspenseTimers,
fetchOptimistic,
shouldSuspend,
suspend,
willFetch,
} from './suspense'
import type {
Expand Down Expand Up @@ -307,7 +308,7 @@ export function useQueries<
: []

if (suspensePromises.length > 0) {
throw Promise.all(suspensePromises)
suspend(Promise.all(suspensePromises))
}
const firstSingleResultWhichShouldThrow = optimisticResult.find(
(result, index) => {
Expand Down
Loading