I'm using Flow 0.30.0. I have a function that returns a promise:
function process(callback:Function):Promise { return new Promise((r,re) => callback) }
Flow raises an error, complaining:
function process(callback:Function):Promise { ^^^^^^^ Promise. Application of polymorphic type needs <list of 1 argument>. (Can use `*` for inferrable ones)
This works in Try Flow, but not locally. Any ideas of the issue?
Promise accepts a callback function as parameters, and in turn, the callback function accepts two other parameters, resolve and reject. If the condition is true, then resolve is returned; else, returns reject. Basically, the return type of Promise type is defined immediately after the keyword Promise.
If a handler function: returns a value, the promise returned by then gets resolved with the returned value as its value. doesn't return anything, the promise returned by then gets resolved with an undefined value. throws an error, the promise returned by then gets rejected with the thrown error as its value.
let promise = new Promise(function(resolve, reject) { setTimeout(() => resolve({msg: 'To do some more job'}), 1000); }); promise. then(function(result) { return {data: 'some data'}; }); promise. then(function(result) { return {data: 'some other data'}; }); promise.
It allows you to associate handlers with an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of immediately returning the final value, the asynchronous method returns a promise to supply the value at some point in the future.
According to the flow changelog there was a change introduced in v0.28.0 which made type arguments explicit. Explicitly setting the return type to Promise<any>
should fix your issue.
Alternatively you can set the following flag to false, although this is a temporary flag which will be removed in the future
experimental.strict_type_args=false
https://github.com/facebook/flow/blob/master/Changelog.md#v0280
I'm guessing this was figured some time ago and the correct answer to what was going on was that it was a versions mismatch between local and the on-line flow interpreter.
However, as this question turns up near the top of search results when looking for how to Flow annotate Promises, it's probably worth mentioning that the fix for these types of situations is to specify the Flow type that the Promise resolves to as a Flow angle bracket parameter.
In this case adding a "any" to the Promise annotation silences the error e.g. ...
function process(callback:Function):Promise<any> { return new Promise((r,re) => callback) }
In the docs over here as well for another example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With