Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Promise as return type in Flow

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?

like image 770
Allyl Isocyanate Avatar asked Aug 02 '16 21:08

Allyl Isocyanate


People also ask

What is a Promise return type?

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.

How do you return a value from 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.

How do I use promises in node JS?

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.

What is the use of promises in JavaScript?

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.


2 Answers

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

like image 182
mike d Avatar answered Sep 28 '22 01:09

mike d


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

like image 34
shufflingb Avatar answered Sep 27 '22 23:09

shufflingb