Skip to content

Commit

Permalink
Fix handling of sync rejection
Browse files Browse the repository at this point in the history
Reverts facebook#14632 and adds a regression test.
  • Loading branch information
gaearon committed Jan 18, 2019
1 parent a2fa6eb commit afbd59a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react-reconciler/src/ReactFiberLazyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export function readLazyComponentType<T>(lazyComponent: LazyComponent<T>): T {
lazyComponent._status = Pending;
const ctor = lazyComponent._ctor;
const thenable = ctor();
lazyComponent._result = thenable;
thenable.then(
moduleObject => {
if (lazyComponent._status === Pending) {
Expand Down Expand Up @@ -77,7 +78,6 @@ export function readLazyComponentType<T>(lazyComponent: LazyComponent<T>): T {
if (lazyComponent._status === Resolved) {
return lazyComponent._result;
}
lazyComponent._result = thenable;
throw thenable;
}
}
Expand Down
31 changes: 31 additions & 0 deletions packages/react-reconciler/src/__tests__/ReactLazy-test.internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ describe('ReactLazy', () => {
expect(root).toMatchRenderedOutput('Hi');
});

it('can handle synchronous thenable rejection', async () => {
const LazyText = lazy(() => ({
then(resolve, reject) {
reject(new Error('oh no'));
},
}));

class ErrorBoundary extends React.Component {
state = {};
static getDerivedStateFromError(error) {
return {message: error.message};
}
render() {
return this.state.message
? `Error: ${this.state.message}`
: this.props.children;
}
}

const root = ReactTestRenderer.create(
<ErrorBoundary>
<Suspense fallback={<Text text="Loading..." />}>
<LazyText text="Hi" />
</Suspense>
</ErrorBoundary>,
);
// TODO: ideally we could make it skip this commit.
expect(ReactTestRenderer).toHaveYielded(['Loading...']);
expect(root).toMatchRenderedOutput('Error: oh no');
});

it('multiple lazy components', async () => {
function Foo() {
return <Text text="Foo" />;
Expand Down

0 comments on commit afbd59a

Please sign in to comment.