Unverified Commit ce5b59d2 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

Remove unnecessary async function when preloading async components (#42957)

The fix in #42793 added an unnecessary step by returning a new async function when the result is a promise. We only have to make sure we don't get an unhandledRejection error if the promise rejects, React takes care of getting the data from the promise.

Ref: [slack thread](https://vercel.slack.com/archives/C035J346QQL/p1668528003500329?thread_ts=1668434306.364449&cid=C035J346QQL)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 58fa90f1
...@@ -59,23 +59,12 @@ function preloadComponent(Component: any, props: any) { ...@@ -59,23 +59,12 @@ function preloadComponent(Component: any, props: any) {
} }
try { try {
let result = Component(props) let result = Component(props)
if (result && result.then) { if (result && typeof result.then === 'function') {
result = result // Catch promise rejections to prevent unhandledRejection errors
.then((res: any) => { result.then(
return { success: res } () => {},
}) () => {}
.catch((err: Error) => { )
return { error: err }
})
return async () => {
const res = await result
if (res.error) {
throw res.error
}
if (res.success) {
return res.success
}
}
} }
return function () { return function () {
// We know what this component will render already. // We know what this component will render already.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment