From 1c532d50fe78d93071e3d730a75c40128472ffb0 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 19 Oct 2022 16:01:07 -0400 Subject: [PATCH] Avoid unhandled rejection errors for Promises that we intentionally ignore In the final passes, we ignore the newly generated Promises and use the previous ones. This ensures that if those generate errors, that we intentionally ignore those. --- packages/react-reconciler/src/ReactFiberHooks.new.js | 7 +++++++ packages/react-reconciler/src/ReactFiberHooks.old.js | 7 +++++++ packages/react-server/src/ReactFizzHooks.js | 5 +++++ packages/react-server/src/ReactFlightHooks.js | 7 +++++++ 4 files changed, 26 insertions(+) diff --git a/packages/react-reconciler/src/ReactFiberHooks.new.js b/packages/react-reconciler/src/ReactFiberHooks.new.js index 244c0f77c6ed6..34fc709fdc388 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.new.js +++ b/packages/react-reconciler/src/ReactFiberHooks.new.js @@ -768,6 +768,8 @@ if (enableUseMemoCacheHook) { }; } +function noop(): void {} + function use(usable: Usable): T { if (usable !== null && typeof usable === 'object') { // $FlowFixMe[method-unbinding] @@ -793,6 +795,11 @@ function use(usable: Usable): T { index, ); if (prevThenableAtIndex !== null) { + if (thenable !== prevThenableAtIndex) { + // Avoid an unhandled rejection errors for the Promises that we'll + // intentionally ignore. + thenable.then(noop, noop); + } switch (prevThenableAtIndex.status) { case 'fulfilled': { const fulfilledValue: T = prevThenableAtIndex.value; diff --git a/packages/react-reconciler/src/ReactFiberHooks.old.js b/packages/react-reconciler/src/ReactFiberHooks.old.js index 283e9daedd046..938cddf02e1bd 100644 --- a/packages/react-reconciler/src/ReactFiberHooks.old.js +++ b/packages/react-reconciler/src/ReactFiberHooks.old.js @@ -768,6 +768,8 @@ if (enableUseMemoCacheHook) { }; } +function noop(): void {} + function use(usable: Usable): T { if (usable !== null && typeof usable === 'object') { // $FlowFixMe[method-unbinding] @@ -793,6 +795,11 @@ function use(usable: Usable): T { index, ); if (prevThenableAtIndex !== null) { + if (thenable !== prevThenableAtIndex) { + // Avoid an unhandled rejection errors for the Promises that we'll + // intentionally ignore. + thenable.then(noop, noop); + } switch (prevThenableAtIndex.status) { case 'fulfilled': { const fulfilledValue: T = prevThenableAtIndex.value; diff --git a/packages/react-server/src/ReactFizzHooks.js b/packages/react-server/src/ReactFizzHooks.js index c8ceeddc400e4..0bd7c252c664b 100644 --- a/packages/react-server/src/ReactFizzHooks.js +++ b/packages/react-server/src/ReactFizzHooks.js @@ -610,6 +610,11 @@ function use(usable: Usable): T { index, ); if (prevThenableAtIndex !== null) { + if (thenable !== prevThenableAtIndex) { + // Avoid an unhandled rejection errors for the Promises that we'll + // intentionally ignore. + thenable.then(noop, noop); + } switch (prevThenableAtIndex.status) { case 'fulfilled': { const fulfilledValue: T = prevThenableAtIndex.value; diff --git a/packages/react-server/src/ReactFlightHooks.js b/packages/react-server/src/ReactFlightHooks.js index fcbcbc0ff142e..1547f29dbcfbe 100644 --- a/packages/react-server/src/ReactFlightHooks.js +++ b/packages/react-server/src/ReactFlightHooks.js @@ -121,6 +121,8 @@ function useId(): string { return ':' + currentRequest.identifierPrefix + 'S' + id.toString(32) + ':'; } +function noop(): void {} + function use(usable: Usable): T { if (usable !== null && typeof usable === 'object') { // $FlowFixMe[method-unbinding] @@ -147,6 +149,11 @@ function use(usable: Usable): T { index, ); if (prevThenableAtIndex !== null) { + if (thenable !== prevThenableAtIndex) { + // Avoid an unhandled rejection errors for the Promises that we'll + // intentionally ignore. + thenable.then(noop, noop); + } switch (prevThenableAtIndex.status) { case 'fulfilled': { const fulfilledValue: T = prevThenableAtIndex.value;