Is there an example of using fetch with flowjs?

I am trying to use fetch in my asynchronous function, but the thread is causing this error

Error: (51, 26) Stream: promise. This type is incompatible with union: enter Promise | type T standby

this is the code that can generate this error:

 async myfunc() { const response = await fetch('example.com'); return await response.json(); } 

I would like to type response.json answer

+5
source share
1 answer

You can either annotate the return type of the function with Promise <T> , where T is the desired type, or assign the result to a temporary local with an explicit type annotation, and then return that local one. Then the type of the returned function will be displayed.

Explicit return type annotation:

 async myfunc(): Promise<{name: string}> { const response = await fetch('example.com'); return await response.json(); } 

Derived a return type from an explicitly annotated local:

 async myfunc() { const response = await fetch('example.com'); const result: {name: string} = await response.json(); return result; } 
+4
source

Source: https://habr.com/ru/post/1258833/


All Articles