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)
This commit is contained in:
Hannes Bornö 2022-11-18 10:51:23 +01:00 committed by GitHub
parent 58fa90f1bf
commit ce5b59d2ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -59,23 +59,12 @@ function preloadComponent(Component: any, props: any) {
}
try {
let result = Component(props)
if (result && result.then) {
result = result
.then((res: any) => {
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
}
}
if (result && typeof result.then === 'function') {
// Catch promise rejections to prevent unhandledRejection errors
result.then(
() => {},
() => {}
)
}
return function () {
// We know what this component will render already.