首页 文章

使用fable在F#中解析Thenable <'t>的语法是什么?

提问于
浏览
3

我'm working on a vscode extension written in F# using Fable to compile to javascript. Many of the api'返回一个承诺 . 解析具有返回类型的承诺的语法是什么,例如 Thenable<string[]> 用于F#?

以下是vscode的许多api的示例:vscode api

2 回答

  • 2

    看看Ionide如何做到这一点:

    https://github.com/ionide/ionide-vscode-helpers/blob/fable/Helpers.fs https://github.com/ionide/ionide-vscode-helpers/blob/fable/Fable.Import.VSCode.fs

    基本上,看起来Ionide几乎忽略了 Thenable<T> 的存在,并在其Fable绑定中将每个API调用转换为 Promise<T> . 它们在Helpers.fs中确实有一对 toPromisetoThenable 函数,但是我没有看到在整个https://github.com/ionide/ionide-vscode-fsharp存储库中的任何地方使用它们 .

    我对寓言没有任何个人经验,所以如果这还不足以回答你的问题,希望其他人可以提供更多信息 .

  • 3

    在玩了一些语法之后,我能够用rmunn给出的将Thenable转换为Promise的线索来弄明白 .

    module PromiseUtils =
      let success (a : 'T -> 'R) (pr : Promise<'T>) : Promise<'R> =
          pr?``then`` (unbox a) |> unbox
    
      let toPromise (a : Thenable<'T>) = a |> unbox<Promise<'T>>
    
      let toThenable (a : Promise<'T>) = a |> unbox<Thenable<'T>>
    

    使用上面的实用程序模块,我能够将返回Thenable的函数转换为Promises,以便它们可以被重新激活 .

    let result = commands.getCommands ()
                   |> PromiseUtils.toPromise
                   |> PromiseUtils.success (fun item -> 
                      let firstOne = item.Item 1
                      console.log(firstOne))
    

相关问题