Hello,
forgive my ignorance, this a very simple (and probably silly) question: how to annotate a function returning a Promise?
I've read most of the official documentation and I haven't found a specific page dedicated to Promises.
I've seen a couple of threads here on github but things are not clear to me.
Also, when we specify a Promises return type how to specify the resolve/reject cases?
Thanks
async function demo(): Promise<string> {
return 'I love cats!';
}
how to specify the resolve/reject cases?
Resolve: See above
Reject: You don't - that's not a return type, that's an exception.
Thanks @lll000111 for the example!
Any link to the official documentation?
You are already looking at it? (You opened in issue in the repo that holds the source.)
https://github.com/facebook/flow/blob/master/lib/core.js#L594
Yes, you are right, I was talking about https://flow.org/en/docs/
Probably it is still not there?
Why don't you go ahead and update it? Contributors welcome :-) That's how I got the "Contributor" badge, I only updated some of that documentation.
Yes I will, but having a very limited experience with Flow I will wait a bit before writing stupid things :]
Hey folks, this topic was extremely helpful to me. So I added a doc page per request - https://github.com/facebook/flow/pull/4927 - did I do that right?
Is there official documentation already? This is over 4months old issue but I still can not find the promises in docs :(.
@VladimirPittner You won't find a lot of things which is hard to duplicate in docs. Just look at lib defs
https://github.com/facebook/flow/blob/master/lib/core.js#L591
@TrySound sorry I do not understand what do you mean by 'hard to duplicate in docs'. If I would want to search for things in actual code, I would not look for documentation, but thanks.
PS: Why don't actually drop all docs and just read the code, its probably self explanatory ;).
how to specify the resolve/reject cases?
Reject: You don't - that's not a return type, that's an exception.
How should you specify what type of argument will be passed to .reject() then?
@nickshanks
There is no provision for a type for reject
because by convention reject
should _always_ get an Error
. Since you can, however, do whatever you want the standard definitions use a hard-coded any
:
reject: (error: any) => void
See https://github.com/facebook/flow/blob/master/lib/core.js#L615
Feel free to define your own version of reject
by redefining class Promise
if you really really want to, but it would be much saver to stick to the very reasonable convention and create a new Error()
when calling reject
.
If you use eslint there is https://eslint.org/docs/rules/prefer-promise-reject-errors (plus rule " no-throw-literal").
I wanted to declare reject: (error: Error) => void
rather than any
.
@nickshanks
You can override anything in the standard definitions coming with flow in your own declaration file in (by default)
PS: Don't forget the static reject
method that is also in there.
I've just realized that #4927 was never merged and Promises are still not very well documented in the docs page. Is there any reason for that?
Sure glad I found this issue, I wasted two hours of my day trying different things.
Most helpful comment
@TrySound sorry I do not understand what do you mean by 'hard to duplicate in docs'. If I would want to search for things in actual code, I would not look for documentation, but thanks.
PS: Why don't actually drop all docs and just read the code, its probably self explanatory ;).