-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Promise type should separate failure and success types separately #1232
Comments
I will submit a patch if such modifications seem acceptable. |
Promises can also fail if any exception is thrown from inside the promise. This is the reason why the error type is any. |
I suppose you could restrict the reject callback argument type, but not the onReject parameter type... |
Sure but I implied that |
Why so I'm not following I'm afraid. |
For example one could write code like: var result:Promise<Buffer, String|TypeError > = new Promise((resolve, reject) => {
fs.readFle(path, (error, data) => error == null ? resolve(data) : reject(error.message))
}); Note that above will produce rejected promise with a TypeError error because there was a typo in Further more I think this kind of errors are handled by flow already. If you do intend on throwing exception manually from the Promise I don't see why you would not encode those possibilities in the type signature. And well if you don't want to you could always say |
I suppose the best way to show this is to give examples. Here are 3 ways that Promises can be "rejected" which would need to be constrained to Explicit
|
That said, while I don't personally believe there is a solution (edit: at least, a solution that doesn't require some significant preparatory work) that affords type safety given the above, I welcome you to attempt it. Happy to review a PR. My overall advice is this: |
Closing this pending a proposal that addresses the concerns I outlined above. |
I agree that this would be a great feature. We've been longing to use a second type parameter in our promises. It's clear, though, from Sam's examples, that flow would have to handle exceptions at the function level before tackling Promises (if that happens at all). In your last Runtime error example, @samwgoldman, you say, " This behavior necessitates that the type parameter at least be bound by Error, which limits its utility." Great observation, but I disagree that we've limited the utility. Rather, I feel you've uncovered a bit of clumsiness with ES6 Promises that flow would have to deal with. I'm wondering if perhaps flow could enforce that developers specify at least a union with |
@unscriptable Fair points, and thanks for the thoughtful discussion. Say we could enforce that (Edit: I'm pretty curious to see the use case for transforming rejection values. Considering this design wart for Promises, I wonder if the better alternative is to roll an |
Just came here looking to see if there was a better story than in TypeScript microsoft/TypeScript#7588. |
What if we allowed Promises to take optional second error type defaulting to |
Why was this closed? |
@samwgoldman outlined in comment above that run-time exceptions could poison soundness. Like right now type guarantees by flow are sound in that even if you throw or run-time exceptions happens types will still match declarations. Unfortunately promises are different in a sense that exceptions will sneak back into run-time or in other words I still think think it would still be improvement over what we have today, but I also understand provided counter arguments as that would alter overall soundness of flow. |
Current type definition of Promise takes single polymorphic parameter
R
that seems to represent type of the value it will be successfully fulfilled with:https://github.com/facebook/flow/blob/master/lib/core.js#L422
Unfortunately this mean error case is untyped:
https://github.com/facebook/flow/blob/master/lib/core.js#L423
Furthermore
onFulfill
handler can return rejected promise and there for make result of.then
rejected promise.I run into this issue with a code like:
Problem is I can no longer restrict errors that task can fail with. Which is why I'd like to propose altering a type definition for promises as follows:
The text was updated successfully, but these errors were encountered: