2

New to Reactive Java here. Can someone please help with this simple scenario (written in Kotlin):

    fun testReactive(): Int {
      Mono.just("item-exists-in-database")
        .onErrorReturn("item-missing") // if item is not in database an exception will be thrown
        .map { item ->
            if (item == "item-exists-in-database") {
                Mono.just("deleting")
                    .doOnSuccess { println("deleting item") } // <-- this never prints!
            } else {
                Mono.just("ok")
            }
        }.map {
            println("Creating item in database")
            Mono.just("creating item in database")
        }.block()
      return 0
}

Running it produces:

Creating item in database

Why doesn't the line "deleting item" ever print?

bubbles
  • 2,597
  • 1
  • 15
  • 40
Praveen Ray
  • 83
  • 1
  • 8
  • You should use flatMap, not map. Map is for converting a value into another value directly. What you do is creating a Mono (container) from a value, adding an indirection level. Look at return type of you ending block() method. – amanin Oct 28 '20 at 09:28

1 Answers1