Skip to content

Fix unhandled promise rejection in onceWithCleanup#2833

Merged
rom1504 merged 2 commits into
PrismarineJS:masterfrom
IceTank:unhandled-promise-rejection-fix
Dec 22, 2022
Merged

Fix unhandled promise rejection in onceWithCleanup#2833
rom1504 merged 2 commits into
PrismarineJS:masterfrom
IceTank:unhandled-promise-rejection-fix

Conversation

@IceTank
Copy link
Copy Markdown
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

Comment thread lib/promise_utils.js Outdated
}

task.promise.finally(() => emitter.removeListener(event, onEvent))
task.promise.finally(() => emitter.removeListener(event, onEvent)).catch((err) => {})
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

IceTank commented Nov 23, 2022

@extremeheat Changed it

@extremeheat
Copy link
Copy Markdown
Member

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

@IceTank
Copy link
Copy Markdown
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