# Mono: map, doOnNext, doOnSubscribe methods

In 
Mono
Published 2022-09-23

This tutorial explains how we can use the map, doOnNext, doOnSubscribe, log methods.

map : could change the value emitted by the Mono the method is associated with.

doOnNext : trigger a code when the value is emitted by the Mono object create before the doOnNext is called (the Mono, the method is associated with).

doOnSubscribe : run a code immediately after the subscription

log : log what is done with the Mono. This is for debugging purpose.

Take a look at the following example created with Spring Boot 3.0, maven in Kotlin:

DemoMonoApplication.java
package com.example.demo1

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import reactor.core.publisher.Mono

@SpringBootApplication
class DemoMonoApplication

suspend fun main(args: Array<String>) {
	runApplication<Demo1Application>(*args)

	fun getMyValue() {
		println("from getMyValue()")
	}

	var val1 : Mono<String?> = Mono.just("A")
		.log()
		// .map -> change the value emitted by the Mono
		.map { it+"A" }
		// .doOnNext -> could run any code
		.doOnNext {
			getMyValue()
		}
		// .doOnNext -> the order is preserved
		.doOnNext(System.out::println)
		.map{it+"BB"}
		.doOnNext(System.out::println)
		// .doOnSubscribe -> this is run when the subscription is created
		.doOnSubscribe{ println("Do something on Subscribe") }

	val1.subscribe { println("Done!") }
}

When we run this code we receive the following output:

2023-09-23T22:31:04.963+03:00  INFO 11404 --- [           main] com.example.demo1.Demo1ApplicationKt     : Started Demo1ApplicationKt in 1.868 seconds (process running for 2.253)
2023-09-23T22:31:04.973+03:00  INFO 11404 --- [           main] reactor.Mono.Just.1                      : | onSubscribe([Synchronous Fuseable] Operators.ScalarSubscription)
Do something on Subscribe
2023-09-23T22:31:04.975+03:00  INFO 11404 --- [           main] reactor.Mono.Just.1                      : | request(unbounded)
2023-09-23T22:31:04.977+03:00  INFO 11404 --- [           main] reactor.Mono.Just.1                      : | onNext(A)
from getMyValue()
AA
AABB
Done!
2023-09-23T22:31:04.978+03:00  INFO 11404 --- [           main] reactor.Mono.Just.1                      : | onComplete()