Skip to content

Conversation

danielrentz
Copy link
Contributor

Fixes #57

This PR contains the following changes:

  • Parameter onrejected of Promise.catch is now required. Calling promise.catch() is technically allowed in JS but rather useless (except for postponing the new promise to the next tick which can still be done with promise.then(null) if really needed).
  • Promise.catch is now generic and allows to add a new type to the resulting promise.

For consistency, Promise.then and PromiseLike.then have been adapted as well:

  • Parameter onfulfilled is now required (see above).
  • Parameter onrejected allows to return another type, like Promise.catch above.
  • Passing a function to onfulfilled changes the resulting promise to the new type (Promise<U>).
  • Passing a function or nullish to onfulfilled widens the resulting promise to the old and new type (Promise<T|U>). This may happen for example when passing a variable containing a callback or null.

One may think to go even further and to allow to return different types from onfulfilled and onrejected (effectively resulting in Promise<U|V> or even Promise<T|U|V> with nullish onfulfilled) but I think this is out of scope of this PR.

Copy link
Owner

@uhyo uhyo left a comment

Choose a reason for hiding this comment

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

Thank you for contribution!

The implementation looks correct and the tests are good too.

@uhyo uhyo merged commit 222baac into uhyo:master Jan 14, 2025
1 check 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.

Promise.catch should allow to widen promise type
2 participants