Fable icon indicating copy to clipboard operation
Fable copied to clipboard

a problem in using promise,

Open napretep opened this issue 2 years ago • 5 comments

Description

I don't know how to show this demo code. So I can only post images

image

the readCardLibReturnCard's return type Promise<Promise<seq<Card>>> does not correspond to the correct js code

To satisfy the type constraint, I had to write the code like this image

Its corresponding compiled js code is image

The error reported by the browser image

I am now using forced type conversion to solve. image But I don't think it's natural to use it this way

I will try to write a demo code later

Repro code

Please provide the F# code to reproduce the problem or a link to the REPL. Ideally, it should be possible to easily turn this code into a unit test.

Expected and actual results

Please provide the expected and actual results.

Related information

  • Fable version: dotnet fable --version
  • Operating system

napretep avatar Sep 05 '22 22:09 napretep

I believe the problem here is Promise<T>.then: when it accepts it a lambda that returns Promise<U> it should unwrap the promise and return Promise<U> as output and not, Promise<Promise<U>>. In order words, then overload in this case should act like bind, not map

@napretep I think it is better to use Promise.map and Promise.bind to get more explicit and correct types. You could even use the promise computation expression to do these:

static member readCardLibReturnId() = promise {
    let! maybeData = DataStorage.read [| CardLib.S |]
    return 
         maybeData[CardLib.S]
         |> Option.map (fun data -> data :?> string array)
         |> Option.defaultValue [| |]
}

which make the code even cleaner

Zaid-Ajaj avatar Sep 05 '22 22:09 Zaid-Ajaj

Yes, the Promise then bindings are there for completeness but it's difficult to represent a function that returns different types in F#, so the recommended way is to use the promise computation expression from the Fable.Promise Nuget package as @Zaid-Ajaj says.

More info here: https://fable.io/fable-promise/documentation/computation-expression.html

alfonsogarciacaro avatar Sep 06 '22 01:09 alfonsogarciacaro

Yes, the Promise then bindings are there for completeness but it's difficult to represent a function that returns different types in F#, so the recommended way is to use the promise computation expression from the Fable.Promise Nuget package as @Zaid-Ajaj says.

More info here: https://fable.io/fable-promise/documentation/computation-expression.html

thank you , but it is really hard to find the doc in your official website. I often have to commit issue in github, maybe we can improve this.

napretep avatar Sep 06 '22 11:09 napretep

Sorry to hear you're having trouble with the documentations @napretep. Docs can always be improved though I'm usually skeptical about putting too much info in the fable.io site because the community is very dynamic so practices change over time. Maybe we can give more relevance to the template section. I have the feeling most users prefer to start with working examples, many get into Fable through the SAFE stack and they already have very good docs. Other community projects like Elmish (and the Elmish Book) and Feliz have great documentation too.

However, it's true that we are missing a good way to handle questions. One way is to open issues in this repo but it can take a few days to get an answer. Another is the Gitter channel, I'm not usually there but I think you can get help faster, but it's difficult to find information afterwards. I wonder if replacing Gitter with a Discourse forum or similar would be helpful. What do you think @MangelMaxime?

alfonsogarciacaro avatar Sep 08 '22 06:09 alfonsogarciacaro

@alfonsogarciacaro

If someone want to setup and maintain a Discourse forum I will be on it too. It is true that it will help search in the history.

Some project also force their user to use StackOverflow to ask questions, etc. but I feel like they do that more to gain "StackOverflow / Internet points/medals". So I think Discourse forum can be a good alternative.

MangelMaxime avatar Sep 08 '22 07:09 MangelMaxime