Skip to content

[Fiber] Deprecate "Throw a Promise" technique #34032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions packages/internal-test-utils/shouldIgnoreConsoleWarn.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use strict';

module.exports = function shouldIgnoreConsoleWarn(format) {
if (
typeof format === 'string' &&
format.startsWith(
'Throwing a Promise to cause it to suspend is deprecated in React',
)
) {
return true;
}
return false;
};
35 changes: 26 additions & 9 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,8 @@ function resetSuspendedWorkLoopOnUnwind(fiber: Fiber) {
resetChildReconcilerOnUnwind();
}

let didWarnForThrowAPromise = false;

function handleThrow(root: FiberRoot, thrownValue: any): void {
// A component threw an exception. Usually this is because it suspended, but
// it also includes regular program errors.
Expand Down Expand Up @@ -2066,19 +2068,34 @@ function handleThrow(root: FiberRoot, thrownValue: any): void {
// case where we think this should happen.
workInProgressSuspendedReason = SuspendedOnHydration;
} else {
// This is a regular error.
const isWakeable =
thrownValue !== null &&
typeof thrownValue === 'object' &&
typeof thrownValue.then === 'function';

workInProgressSuspendedReason = isWakeable
? // A wakeable object was thrown by a legacy Suspense implementation.
// This has slightly different behavior than suspending with `use`.
SuspendedOnDeprecatedThrowPromise
: // This is a regular error. If something earlier in the component already
// suspended, we must clear the thenable state to unblock the work loop.
SuspendedOnError;
if (isWakeable) {
// A wakeable object was thrown by a legacy Suspense implementation.
// This has slightly different behavior than suspending with `use`.
workInProgressSuspendedReason = SuspendedOnDeprecatedThrowPromise;
if (__DEV__) {
if (!didWarnForThrowAPromise && workInProgress !== null) {
didWarnForThrowAPromise = true;
const componentName =
getComponentNameFromFiber(workInProgress) || 'unknown';
runWithFiberInDEV(workInProgress, () => {
console.warn(
'Throwing a Promise to cause it to suspend is deprecated in React.\n' +
'Please update your library to call use(promise) instead.\n' +
'See https://react.dev/reference/react/use\n\n in %s',
componentName,
);
});
}
}
} else {
// This is a regular error. If something earlier in the component already
// suspended, we must clear the thenable state to unblock the work loop.
workInProgressSuspendedReason = SuspendedOnError;
}
}

workInProgressThrownValue = thrownValue;
Expand Down
Loading