24 people like it.

Async function that retries work

A simple asynchronous workflow that retries running a given async workflow (until "resultOk" function returns true or until a specified number of retries is performed). The function uses tail-recursion in async workflows.

1: 
2: 
3: 
4: 
let rec retry work resultOk retries = async {
  let! res = work
  if (resultOk res) || (retries = 0) then return res
  else return! retry work resultOk (retries - 1) }
val retry : work:Async<'a> -> resultOk:('a -> bool) -> retries:int -> Async<'a>

Full name: Script.retry
val work : Async<'a>
val resultOk : ('a -> bool)
val retries : int
val async : AsyncBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val res : 'a
Raw view Test code New version

More information

Link:http://fssnip.net/hr
Posted:11 years ago
Author:Tomas Petricek
Tags: async , tail-recursion , retry