How to write this C # async IO code in F #?

I have this code:

public async Task CreateFileAsync(string filePath, byte[] bytes)
    {
        using (var sourceStream = System.IO.File.Open(filePath, FileMode.OpenOrCreate))
        {
            sourceStream.Seek(0, SeekOrigin.End);
            await sourceStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
        }
    }

      

I want to write it in F #, I get to this before I cant figure out what to do:

module myModule
open System.IO;
let CreateFileAsync (filePath: string, bytes : byte[]) =
    use sourceStream = File.Open(filePath, FileMode.OpenOrCreate)
        |> sourceStream.Seek(0, SeekOrigin.End);        

      

I've searched around, but there are a couple of concepts here and I can't seem to put them all together.

+3


source to share


1 answer


You can use a workflow async { .. }

:

let createFileAsync (filePath, bytes) = async {
  use sourceStream = System.IO.File.Open(filePath, FileMode.OpenOrCreate)
  sourceStream.Seek(0, SeekOrigin.End)
  do! sourceStream.AsyncWrite(bytes, 0, bytes.Length) }

      



This is pretty imperative code, so it doesn't look very different in F #.

+3


source







All Articles