我习惯使用Scala编程语言-使用Scala可以映射期货,例如:
val response: Future[HttpResponse] = asyncHttpClient.GetRequest("www.google.com")
val statusCode: Future[Int] = response.map(r => r.statusCode)
最近,我开始使用C#,并且发现自己处在与上述示例相同的情况下,但是我无法弄清楚如何“映射”任务。
这是我要实现的示例:
Task<HttpResponseMessage> response = httpClient.GetAsync("www.google.com")
Task<int> statusCode = response.Map(response => response.StatusCode)
谢谢
使用现有方法最直接的翻译是:
Task<int> statusCode = response.ContinueWith(t => t.Result.StatusCode)
但是实际上,您几乎总是在等待任务以获取结果。也许您应该考虑异步/等待。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句