Unverified Commit fc3c96d3 authored by Jeremie's avatar Jeremie Committed by GitHub

Fix unhandled rejection in proxy-request (#72530)

Trying to fix : https://github.com/vercel/next.js/issues/72522

### What?
The finally method actually creates a new promise. You have to listen to
this new promise instead of the original one or you might end up with
unhandled rejection.

Taken from the documentation : 
> The finally() method of
[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
instances schedules a function to be called when the promise is settled
(either fulfilled or rejected). It immediately returns another
[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
object, allowing you to
[chain](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#chaining)
calls to other promise methods.

### Why?
see : https://github.com/vercel/next.js/issues/72522

### How?

Fixes : https://github.com/vercel/next.js/issues/72522Co-authored-by: 's avatarWyatt Johnson <accounts+github@wyattjoh.ca>
parent 7fdbabca
...@@ -77,11 +77,6 @@ export async function proxyRequest( ...@@ -77,11 +77,6 @@ export async function proxyRequest(
const detached = new DetachedPromise<boolean>() const detached = new DetachedPromise<boolean>()
// When the proxy finishes proxying the request, shut down the proxy.
detached.promise.finally(() => {
proxy.close()
})
proxy.on('error', (err) => { proxy.on('error', (err) => {
console.error(`Failed to proxy ${target}`, err) console.error(`Failed to proxy ${target}`, err)
if (!finished) { if (!finished) {
...@@ -125,5 +120,8 @@ export async function proxyRequest( ...@@ -125,5 +120,8 @@ export async function proxyRequest(
}) })
} }
return detached.promise // When the proxy finishes proxying the request, shut down the proxy.
return detached.promise.finally(() => {
proxy.close()
})
} }
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