Reputation: 24472
My program services offer some delete methods that return Mono<Void>
, e.g.: fun delete(clientId: String) : Mono<Void>
After calling .delete("x")
I would like to propagate the clientId downstream to do other operations:
userService.get(id).map{ user ->
userService.delete(user.id) //This returns Mono<Void>
.map {
user.id //Never called!!!
}
.map { userId ->
//other calls using the propagated userId
}
}
The problem is since delete returns a Mono<Void>
, the following .map {
user.id }
is never called. So how can I transform the Mono<Void>
into a Mono<String>
to propagate the userId?
Upvotes: 4
Views: 7162
Reputation: 9987
You can use thenReturn
operator:
userService.get(id)
.flatMap { user -> userService.delete(user.id).thenReturn(user.id) }
.flatMap { id -> //other calls using the propagated userId }
Upvotes: 14
Reputation: 24472
I managed to work around it using hasNext that transforms it into a Boolean:
@Test
fun `should`() {
val mono: Mono<String> = "1".toMono()
.flatMap { id ->
val map = Mono.empty<Void>()
.hasElement()
.map {
id + "a"
}.map {
(it + "1")
}
map
}
mono.doOnNext {
println(mono)
}.subscribe()
}
Upvotes: 1