Skip to content

handle rejection promises gracefully #15

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

Merged

Conversation

Germandrummer92
Copy link
Contributor

@Germandrummer92 Germandrummer92 commented Jun 28, 2024

closes #14.

  • ran npm audit fix
  • one should never have unawaited promises as they will lead to UnhandledPromiseRejections if they reject in the time UNTIL they will be awaited. This fixes that, see test case.

Comment on lines +20 to +28
this.storedPromise = promise.then(
(value) => {
this.value = { type: "resolved", value };
return;
},
(reason: unknown) => {
this.value = { type: "rejected", reason };
}
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't see a reason for not using async await syntax here, same below for the promise getter

Copy link
Contributor

@kosta kosta Jul 11, 2024

Choose a reason for hiding this comment

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

Here, we want to store a promise that can never reject (because the rejection is caught). So we'd need to do something like

this.storedPromise = (async()=>{
  try {
    const value = await promise;
    return { type: "resolved", value };
  } catch(reason: unknown) {
    return { type: "rejected", reason };
})()

I find the "iffy" syntax a bit weird in typescript, but yeah that would work the same.

About the promise getter: Yeah await syntax would make this a bit nicer but IIRC there are no async getters (yet?), so it would no longer be a getter. Which is fine for me.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, that's what I had in mind, but I understand that one can find it "iffy" :D

will merge it tomorrow or on monday or so

Copy link
Contributor

Choose a reason for hiding this comment

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

:D

Shall I replace the get promise() with async promise()?

Copy link
Collaborator

Choose a reason for hiding this comment

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

no, it's fine :)

@marc-at-work
Copy link
Collaborator

your description: "if they reject in the time UNTIL they will be rejected" should probably be "if they reject in the time UNTIL they will be handled"? :D

@kosta
Copy link
Contributor

kosta commented Jul 11, 2024

your description: "if they reject in the time UNTIL they will be rejected" should probably be "if they reject in the time UNTIL they will be handled"? :D

Yes! I think @Germandrummer92 fixed it already.

@marc-at-work marc-at-work merged commit cb867a8 into understand-ai:main Jul 15, 2024
2 checks passed
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.

unhandled promise rejections
3 participants