Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When writing F# for Fable, when should I use async and when should I use promise?

Tags:

f#

fable-f#

F# with Fable provides two computation expressions for working with callback based code: async and promise.

async is more idomatic F#, but it is not clear to me how well it will work in the browser. Ideally I would be able to use async everywhere, since this gives me greater code reuse across client & server.

  • What are the limitations of async in the browser?
  • What are the limitations of promise on the server (e.g. dotnet core)
  • Can I easily switch between the two? (e.g. to wrap fetch API)
  • What is idomatic for Fable?

Note: I do not need to interop with any JavaScript, aside from the browser API.

like image 695
sdgfsdh Avatar asked Jan 02 '19 16:01

sdgfsdh


People also ask

How is cursive f written?

Begin the lowercase f below the center, dotted line. Bring your stroke up to the top line and loop back around the left side and down below the bottom line. Once below the bottom line, bring your stroke back up the right side and make a small tail off of the letter, towards the right to connect with the other letters.


1 Answers

What are the limitations of async in the browser?

When using async in the browser, you can't use Async.RunSynchronously. I think this is a limitation because JavaScript is single-threaded.

What are the limitations of promise on the server (e.g. dotnet core)

You can't use promise on the server because .Net or .NetCore doesn't know this computation. When working with asynchronous code on a .Net or .NetCore server you need to use async.

If the server, is using Node.js the same limitation applied as in the browser.

Can I easily switch between the two? (e.g. to wrap fetch API)

The fetch API has already been wrapped using promise in Fable.Fetch and Fable.Promise .

    promise {
        let! res = fetch "http://fable.io" []
        let! txt = res.text()
        // Access your resource here
        Browser.console.log txt
    }

Also by opening Fable.Core you can gain access to Async.AwaitPromise and Async.StartAsPromise.

What is idiomatic for Fable?

Personally, I am using promise only because it's a native feature from JavaScript now and in general JavaScript library expects you to work using promise even if you can move between promise and async.

like image 70
Maxime Mangel Avatar answered Sep 21 '22 12:09

Maxime Mangel