Alan Wayne
Alan Wayne

Reputation: 5394

In F#, how to always execute a bound Async process?

I have the following methods in F#:

let GetAllPatientNamesAsync() =
        async {
                let data = context.GetAllPatientNamesAsync() 
                return! Async.AwaitTask data
            }

let getAllPatientNames  = Async.RunSynchronously (FsNetwork.GetAllPatientNamesAsync())

This works the FIRST time:

{ m with names = getAllPatientNames}

But once getAllPatientNames has a value, calling it again does not execute the Async.RunSynchronously (FsNetwork.GetAllPatientNamesAsync()) -- rather getAllPatientNames returns its original value.

Since FsNetwork.GetAllPatientNamesAsync() is accessing the database, which is changing, after the first read getAllPatientNames is wrong.

Ofcourse, this works correctly no matter how many times I use it:

{ m with names = Async.RunSynchronously (FsNetwork.GetAllPatientNamesAsync()) }.

How should I write

let getAllPatientNames  = Async.RunSynchronously (FsNetwork.GetAllPatientNamesAsync())

such that it ALWAYS executes the Async process? What am I doing wrong?

TIA

Upvotes: 0

Views: 42

Answers (1)

user1981
user1981

Reputation: 586

The problem is that

getAllPatientNames

is a a value and not a function. Make a it a function by changing it like below

let getAllPatientNames () = Async.RunSynchronously (FsNetwork.GetAllPatientNamesAsync())

Upvotes: 4

Related Questions