项目反应堆.Mono.map() 与 Mono.flatMap() [英] Project Reactor. Mono.map() vs Mono.flatMap()
问题描述
就Mono
而言,它们之间的主要区别是什么?从文档中,我读到 flatMap
是异步的,而 map
是同步的.但这对我来说并没有真正意义 b/c Mono 是关于并行性的,这一点是不可理解的.有人可以用更容易理解的方式重新表述它吗?
What is the principal difference between these in terms of Mono
?
From the documentation, I read that flatMap
acts asynchronous and map
synchronous. But that doesn't really make sense for me b/c Mono is all about parallelism and that point isn't understandable. Can someone rephrase it in a more understandable way?
然后在 flatMap
的文档中说明 (https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#flatMap-java.util.function.功能-):
Then in the documentation for flatMap
stated (https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html#flatMap-java.util.function.Function-):
Transform the item emitted by this Mono asynchronously, returning the
value emitted by another Mono (possibly changing the value type).
另一个 Mono 是什么意思?
Which another Mono is meant there?
推荐答案
Mono#flatMap
使用 Function
将一个值转换为另一个 Mono
>.Mono 可以代表一些异步处理,例如 HTTP 请求.
Mono#flatMap
takes a Function
that transforms a value into another Mono
. That Mono could represent some asynchronous processing, like an HTTP request.
另一方面,Mono#map
接受一个 Function
将 T
类型的值转换为另一个值,即输入 R
.因此,该转换是命令式和同步完成的(例如,将 String
转换为 URL
实例).
On the other hand, Mono#map
takes a Function
that transforms a value of type T
into another value, of type R
. That transformation is thus done imperatively and synchronously (eg. transforming a String
into an URL
instance).
flatMap
的另一个微妙之处是操作符订阅生成的 Mono
,这与将相同的 Function
传递给地图
.
The other subtlety with flatMap
is that the operator subscribes to the generated Mono
, unlike what would happen if you passed the same Function
to map
.
这篇关于项目反应堆.Mono.map() 与 Mono.flatMap()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!