是否有用于 Promises 的计算表达式构建器?
Is there a computation expression builder for Promises?
async function showSampleText(context: vscode.ExtensionContext): Promise<void> {
let sampleTextEncoded = await vscode.workspace.fs.readFile(vscode.Uri.file(context.asAbsolutePath('sample.txt')));
let sampleText = new TextDecoder('utf-8').decode(sampleTextEncoded);
let doc = await vscode.workspace.openTextDocument({ language: 'plaintext', content: sampleText });
vscode.window.showTextDocument(doc);
}
我将上面的 Typecript 片段翻译成下面的 Fable 代码。
type ITextDecoder =
abstract member decode: ?input: Uint8Array * ?options: {| stream : bool |} -> string
[<Emit("new TextDecoder([=11=])")>]
let TextDecoder label : ITextDecoder = jsNative
let show_sample_text (ctx : Vscode.ExtensionContext) =
async {
let! sampleTextEncoded = Vscode.workspace.fs.readFile(statics.Uri.file(ctx.asAbsolutePath("sample.txt"))) |> Async.AwaitPromise
let sampleText = TextDecoder("utf-8").decode(sampleTextEncoded);
let! doc = workspace.openTextDocument(options= !!{| language="plaintext"; content=sampleText |}) |> Async.AwaitPromise
let! _ = window.showTextDocument(document=doc,options=undefined) |> Async.AwaitPromise
return ()
} |> Async.StartAsPromise
我在 Async
和 Promise
之间显式转换的方式让我很困扰。某处是否有专门针对 Promises 的构建器?
Fable 通过 Fable.Promise 包支持 promise
。
如果您将 Fable.Promise 包添加到您的项目中,那么您可以这样做:
let private getRandomUser () = promise {
// Do something here
// ...
return ()
}
async function showSampleText(context: vscode.ExtensionContext): Promise<void> {
let sampleTextEncoded = await vscode.workspace.fs.readFile(vscode.Uri.file(context.asAbsolutePath('sample.txt')));
let sampleText = new TextDecoder('utf-8').decode(sampleTextEncoded);
let doc = await vscode.workspace.openTextDocument({ language: 'plaintext', content: sampleText });
vscode.window.showTextDocument(doc);
}
我将上面的 Typecript 片段翻译成下面的 Fable 代码。
type ITextDecoder =
abstract member decode: ?input: Uint8Array * ?options: {| stream : bool |} -> string
[<Emit("new TextDecoder([=11=])")>]
let TextDecoder label : ITextDecoder = jsNative
let show_sample_text (ctx : Vscode.ExtensionContext) =
async {
let! sampleTextEncoded = Vscode.workspace.fs.readFile(statics.Uri.file(ctx.asAbsolutePath("sample.txt"))) |> Async.AwaitPromise
let sampleText = TextDecoder("utf-8").decode(sampleTextEncoded);
let! doc = workspace.openTextDocument(options= !!{| language="plaintext"; content=sampleText |}) |> Async.AwaitPromise
let! _ = window.showTextDocument(document=doc,options=undefined) |> Async.AwaitPromise
return ()
} |> Async.StartAsPromise
我在 Async
和 Promise
之间显式转换的方式让我很困扰。某处是否有专门针对 Promises 的构建器?
Fable 通过 Fable.Promise 包支持 promise
。
如果您将 Fable.Promise 包添加到您的项目中,那么您可以这样做:
let private getRandomUser () = promise {
// Do something here
// ...
return ()
}