thenDefer

fun <R> Mono<*>.thenDefer(defer: () -> Mono<R>): Mono<R>

Chains a deferred Mono creation after this Mono completes.

This extension function allows you to defer the creation of a subsequent Mono until the current Mono completes successfully. The defer function is only called after this Mono emits completion, providing lazy evaluation of the next step.

This is useful for scenarios where you want to perform an operation only after the current operation succeeds, and you want to delay the creation of the next Mono (e.g., for database queries, HTTP calls, or other expensive operations).

Example usage:

val result = userRepository.save(user)
.thenDefer {
// This Mono is only created after save completes
emailService.sendWelcomeEmail(user.email)
}
.thenDefer {
// This is only created after email sending completes
auditService.logUserCreation(user.id)
}

Return

a Mono that emits the result of the deferred Mono

Parameters

R

the type of the resulting Mono

defer

a function that creates the next Mono when called

See also

Mono.defer