我正在使用Fable編寫的JavaScript編寫的使用F#編寫的vscode擴展。許多api都會返回一個承諾。解決F#返回類型如Thenable<string[]>
的承諾的語法是什麼?使用寓言解決F#中Thenable <'t>的語法是什麼?
這裏是許多API的爲vscode的例子:vscode api
我正在使用Fable編寫的JavaScript編寫的使用F#編寫的vscode擴展。許多api都會返回一個承諾。解決F#返回類型如Thenable<string[]>
的承諾的語法是什麼?使用寓言解決F#中Thenable <'t>的語法是什麼?
這裏是許多API的爲vscode的例子:vscode api
在玩了一些語法之後,我能夠弄清楚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的函數轉換爲Promise,以便它們可以被重新使用。
let result = commands.getCommands()
|> PromiseUtils.toPromise
|> PromiseUtils.success (fun item ->
let firstOne = item.Item 1
console.log(firstOne))
看看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中有一對toPromise
和toThenable
函數,但我沒有看到那些在整個https://github.com/ionide/ionide-vscode-fsharp存儲庫中的任何地方使用的函數。
我對寓言沒有任何個人經驗,所以如果這還不足以回答你的問題,希望別人能夠提供更多信息。
是的,我看到了這些例子。然而,我沒有真正看到承諾如何實際解決(就像你提到他們沒有被使用),因爲類型不匹配。我肯定錯過了什麼。 – Korbin