Tcp server messenger awaiter in f# Hi I have a simple tcp server, and that server receives messages with specific length so basically i need to wait for TcpClient to fill buffer with that amount of data, and it works. 

But I am not sure if this should look like it looks as I am writing this in f# and still learning:

let private WaitForData (client : TcpClient, dataLength : int) : Async&lt;bool&gt;= 

    async {

        let mutable loopBreaker : bool = true
        let mutable result : bool = false

        while loopBreaker do
            let isConnectionAlive : bool = IsConnectionEstablished client &amp;&amp; client &lt;&gt; null

            if isConnectionAlive &amp;&amp; client.Available &gt;= dataLength then 
                result &lt;- true
                loopBreaker &lt;- false


            if isConnectionAlive = false then 
                result &lt;- false
                loopBreaker &lt;- false                    

            Task.Delay(500) 
            |&gt; Async.AwaitTask 
            |&gt; ignore

        return result
    }


So as you see I use mutables to exit while loop and set result value, and have some if there. 

Is this OK or maybe there is some more elegant way to write this
The first thing is when you use parenthesis it means that you pass a tuple to a function instead of single parameters. So you can ditch them.

The second thing is that bool supports pattern matching

The third thing is that you can use recursion instead of mutable variables. Note the usage of rec which enables tail call optimisation. 

The code would look something like this. 

let rec private WaitForData2 (client : TcpClient) (dataLength : int) (loopBreaker : bool) (result: bool) : Async&lt;bool&gt;= 
    async {
        match loopBreaker with
        | true -&gt; 
            let isConnectionAlive : bool = IsConnectionEstablished client &amp;&amp; client &lt;&gt; null
            match isConnectionAlive, client.Available &gt;= dataLength with
            | true, true -&gt;             
                Task.Delay(500) 
                |&gt; Async.AwaitTask 
                |&gt; ignore
                return! WaitForData2 client dataLength false true                
            | false, _ -&gt; 
                Task.Delay(500) 
                |&gt; Async.AwaitTask 
                |&gt; ignore
                return! WaitForData2 client dataLength false false
            | _, _ -&gt; 
                Task.Delay(500) 
                |&gt; Async.AwaitTask 
                |&gt; ignore
                return! WaitForData2 client dataLength true false
        | false -&gt; return result