2010-11-29 51 views
0

有沒有簡化以下的方法,所以我不需要runWithTimeout函數?參數和管道右側運算符的順序

let runWithTimeout timeout computation = 
    Async.RunSynchronously(computation, timeout) 

let processOneItem item = fetchAction item 
          |> runWithTimeout 2000 

編輯: 這也是爲什麼我需要額外的方法:

let processOneItem item = fetchAction item 
          |> Async.Catch 
          |> runWithTimeout 2000 
          |> handleExceptions 
+1

恐怕不清楚你在問什麼。你是否想知道最簡潔的方式來編寫`let processOneItem item = Async.RunSynchronously(fetchAction item,2000)`? – 2010-11-29 22:09:57

回答

3

也許你的意思是這樣:

let processOneItem item = 
    fetchAction item 
    |> fun x -> Async.RunSynchronously(x, 2000) 
0

下面是一個更完整的樣本,以供將來參考:

let processOneItem item = fetchAction item 
          |> Async.Catch 
          |> fun x -> Async.RunSynchronously(x,30000) 
          |> handleExceptions 
3

我不是很喜歡使用fun x -> ...作爲管道的一部分。

我認爲編寫代碼的流水線風格在API(如列表)支持時很好,但是當API不適合風格時,最好遵循通常的「類C# 「風格。對於coures,這只是個人偏好,但我只是寫:

let processOneItem item = 
    let work = Async.Catch(fetchAction item) 
    let result = Async.RunSynchronously(work, 30000) 
    handleExceptions result