如何忽略异步块中异步函数的返回值? [英] How to ignore the return value of an Async function in an Async block?

查看:107
本文介绍了如何忽略异步块中异步函数的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下功能中的m1m2具有编译错误.

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在最后一行被调​​用.如何忽略其返回值?以下是唯一的方法(编译器会优化它的执行吗?)?

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 
      () 
    }

推荐答案

您可以使用

You can use Async.Ignore for this:

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

从文档中:

Async.Ignore 创建运行给定计算并忽略其结果的异步计算.

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

这篇关于如何忽略异步块中异步函数的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆