Project Reactor. Mono.map() vs Mono.flatMap()

不羁的心 提交于 2020-08-27 07:10:11

问题


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?

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).

Which another Mono is meant there?


回答1:


Mono#flatMap takes a Function that transforms a value into another Mono. That Mono could represent some asynchronous processing, like an HTTP request.

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).

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.



来源:https://stackoverflow.com/questions/56496426/project-reactor-mono-map-vs-mono-flatmap

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