Say I want to call a webservice1 and then call webservice2 if the first was successful.
I can do the following (just indicative psuedo code) :-
Mono.just(reqObj)
.flatMap(r -> callServiceA())
.then(() -> callServiceB())
or
Mono.just(reqObj)
.flatMap(r -> callServiceA())
.flatMap(f -> callServiceB())
What is the difference between the two, when using the mono.just() for a single element?
flatMap
should be used for non-blocking operations, or in short anything which returns back Mono, Flux.
map
should be used when you want to do the transformation of an object /data in fixed time. The operations which are done synchronously.
For ex:
return Mono.just(Person("name", "age:12"))
.map { person ->
EnhancedPerson(person, "id-set", "savedInDb")
}.flatMap { person ->
reactiveMongoDb.save(person)
}
then
should be used when you want to ignore element from previous Mono and want the stream to be finised