The following code was about to first delete a KV from redis, and then set a new KV there and return. But it actually never set
after delete
. I only see mistake after print every log
, as 2 any is MonoNext
. Then I released a chain step before this any
needs to split off one Mono
, so I used flatMap
instead of map
at marker. This problem is more likely happened to me, especially when functions are deeply encapsulated inside another function with Mono
in and out.
@RequestMapping("addUserSimple3")
public Mono<AddUserResponse> addUser3(@RequestBody Mono<AddUserVo> vo) {
return vo.flatMap(v ->
redisOps.delete(v.getGroupId())
.map(any -> { <---------------------Use flatMap instead of map
log.info("1 any is {}", any);
return redisOps.set(v.getGroupId(), v.getPersonId());
})
.map(any ->
{
log.info("2 any is {}", any);
return new AddUserResponse(ResponseState.GOOD, v.getLogId());
})
);
}
Then my question is, how to better understand MonoNext
and flatMap
? (aka, I let the above code work by replacing map
but I don't understand why it did not work, and why it does later)