Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unhandled promise rejection in onceWithCleanup #2833

Merged
merged 2 commits into from
Dec 22, 2022

Conversation

IceTank
Copy link
Contributor

@IceTank IceTank commented Nov 15, 2022

From MDN:

Returns an equivalent Promise with its finally handler set to the specified function. If the handler throws an error or returns a rejected promise, the promise returned by finally() will be rejected with that value instead

So finally will throw an unhandled promise rejection if the task promise rejects. Causing a un-catchable promise rejection on the onceWithCleanup function

@@ -67,7 +67,7 @@ function onceWithCleanup (emitter, event, { timeout = 0, checkCondition = undefi
})
}

task.promise.finally(() => emitter.removeListener(event, onEvent))
task.promise.finally(() => emitter.removeListener(event, onEvent)).catch((err) => {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seem right. You're silently letting this fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The promise returned by finally is a copy off the promise the task itself will return. This is why there is a unhandled promise rejection in the first place. No one is handling the promise returned by finally because it is also returned by the promise in task.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Altho this does not work with the linter. Maybe promise.catch(() => {}).finally(() => ...) would be better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re right, I missed that. It’s a chained promise, so it should be catching error removing the event (if I’m not mistaken)

@IceTank
Copy link
Contributor Author

IceTank commented Nov 23, 2022

@extremeheat Changed it

@extremeheat
Copy link
Member

Why did you change it ? I said you were right.

@IceTank
Copy link
Contributor Author

IceTank commented Nov 23, 2022

I changed it because the old version caused a lint error. This version works.

@rom1504 rom1504 merged commit 6e4c232 into PrismarineJS:master Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants