问题
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