gpt4 book ai didi

f# - 为 Fable 编写 F# 时,什么时候应该使用异步,什么时候应该使用 promise?

转载 作者:行者123 更新时间:2023-12-03 09:44:34 26 4
gpt4 key购买 nike

F# with Fable 提供了两个计算表达式来处理基于回调的代码:asyncpromise .
async更像 idomatic F#,但我不清楚它在浏览器中的工作情况。理想情况下,我可以使用 async无处不在,因为这让我在客户端和服务器之间有更多的代码重用。

  • async有什么限制在浏览器中?
  • promise有什么限制在服务器上(例如 dotnet 核心)
  • 我可以在两者之间轻松切换吗? (例如包装 fetch API)
  • 寓言的惯用语是什么?

  • 注意:除了浏览器 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)



    fetch 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 的 native 功能,并且一般来说 JavaScript 库希望您使用 promise 工作。即使您可以在 promise 之间移动和 async .

    关于f# - 为 Fable 编写 F# 时,什么时候应该使用异步,什么时候应该使用 promise?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54009694/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com