Async.AwaitTask that re-evaluates Task

I’m calling GetAsync on an HttpClient and then using it with Async.AwaitTask. I am then piping that Async<HttpResponseMessage> into some retry logic, which should re-evaluate the Async<HttpResponseMessage> if it has a non-200 status code. However, it retains its Task-like behavior (using the completed/cached result) instead of making the call again (like I would expect if it were a normal Async. Does anybody know of an AwaitTask implementation that provides the behavior I’m looking for, where the Task will be re-evaluated also?

Thanks!

I ended up solving this by putting the creation of the Task into an Async. Curious to know if there’s another way of doing this though.

It’s going to be very hard, because by definition a Task is an one-time action. It caches the result of that single run in the Result property, and the Task itself has no idea what caused it to be spawned (ie is it an http-request Task, or a database call, etc). If you want to be able to use a retry-able task, you either need to have some kind of Task-generator (a function of type unit -> Task<'t>) that gets passed into your initial Async-creating function, or rewrite the operation in terms of an Async in the first place.

1 Like