Skip to content

Commit f7d5353

Browse files
author
Sebastian Silbermann
committed
DevTools: Add support for use(Promise)
1 parent 79bf36f commit f7d5353

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
ReactContext,
1313
ReactProviderType,
1414
StartTransitionOptions,
15+
Thenable,
1516
Usable,
1617
} from 'shared/ReactTypes';
1718
import type {
@@ -122,14 +123,33 @@ function readContext<T>(context: ReactContext<T>): T {
122123
return context._currentValue;
123124
}
124125

126+
function useThenable<T>(thenable: Thenable<T>): T {
127+
switch (thenable.status) {
128+
case 'fulfilled':
129+
return thenable.value;
130+
case 'rejected':
131+
throw thenable.reason;
132+
case 'pending':
133+
default:
134+
throw thenable;
135+
}
136+
}
137+
125138
function use<T>(usable: Usable<T>): T {
126139
if (usable !== null && typeof usable === 'object') {
127140
// $FlowFixMe[method-unbinding]
128141
if (typeof usable.then === 'function') {
129-
// TODO: What should this do if it receives an unresolved promise?
130-
throw new Error(
131-
'Support for `use(Promise)` not yet implemented in react-debug-tools.',
132-
);
142+
// This is a thenable.
143+
const thenable: Thenable<T> = (usable: any);
144+
const value = useThenable(thenable);
145+
146+
hookLog.push({
147+
primitive: 'Use',
148+
stackError: new Error(),
149+
value,
150+
});
151+
152+
return value;
133153
} else if (usable.$$typeof === REACT_CONTEXT_TYPE) {
134154
const context: ReactContext<T> = (usable: any);
135155
const value = readContext(context);

0 commit comments

Comments
 (0)