写F# for Fable,什么时候用async,什么时候用promise?

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

F# with Fable 提供了两个用于处理基于回调的代码的计算表达式:asyncpromise

async 更像 F#,但我不清楚它在浏览器中的工作情况。理想情况下,我可以在任何地方使用 async,因为这使我在客户端和服务器之间可以更好地重用代码。

注意:除了浏览器 API,我不需要与任何 JavaScript 互操作。

What are the limitations of async in the browser?

在浏览器中使用async时,不能使用Async.RunSynchronously。我认为这是一个限制,因为 JavaScript 是单线程的。

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

您不能在服务器上使用 promise,因为 .Net 或 .NetCore 不知道这种计算。在 .Net 或 .NetCore 服务器上使用异步代码时,您需要使用 async.

如果服务器正在使用 Node.js 与浏览器相同的限制。

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

提取 API 已经在 Fable.Fetch 和 Fable.Promise 中使用 promise 包装。

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

另外,通过打开 Fable.Core,您可以访问 Async.AwaitPromiseAsync.StartAsPromise

What is idiomatic for Fable?

就个人而言,我使用 promise 只是因为它现在是 JavaScript 的原生功能,而且一般来说 JavaScript 库希望您使用 promise,即使您可以在 promiseasync 之间移动。