How to ignore the return value of an Async function in an Async block?

孤人 提交于 2020-03-02 07:59:50

问题


The m1 and m2 in the following functions have compiling errors.

let m p = async { return p * 2 }
let m1 () = async { do! m 2 } // ERR: was expected 'int' but here has type 'unit'
let m2 () = async { do! m 2 |> ignore } // ERR: expecting 'Async<int>->Async<'a>' but given 'Async<int>->unit'

m is called at the last line. How to ignore its return value? Is the following the only way (will executing of it be optimized by the compiler?)?

let m1 () = 
    async { 
      let! x = m 2 
      () 
    }

回答1:


You can use Async.Ignore for this:

let m1 () = async { do! m 2 |> Async.Ignore }

From the documentation:

Async.Ignore Creates an asynchronous computation that runs the given computation and ignores its result.



来源:https://stackoverflow.com/questions/50762436/how-to-ignore-the-return-value-of-an-async-function-in-an-async-block

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!