Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
What is the correct way to convert a Kotlin suspend function to a function returning Mono?

Let's say we have a function

suspend fun doSomething(value: Int): String {
    delay(1000L)
    return "abc_$value"
}

How to convert it to a function returning Mono? Are there any hidden problems with switching between threads belonging to coroutine scope and reactor event loop threads?

fun resultAsMono(fn: suspend (Int) -> String): (Int) -> Mono<String> {
   // ???
}

So that effect would be like this:

val newFn = resultAsMono(::doSomething)

val result = newFn(5).block()

assertThat(result).isEqualTo("abc_5")
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

A function already exists for this conversion. You need this dependency...

<dependency>
    <groupId>org.jetbrains.kotlinx</groupId>
    <artifactId>kotlinx-coroutines-reactor</artifactId>
    <version>1.6.0</version>
</dependency>

...and then you can do the following:

import kotlinx.coroutines.delay
import kotlinx.coroutines.reactor.mono

fun main() {
    val mono = mono { doSomething(5) }
    val result = mono.block()
    println(result)
}

suspend fun doSomething(value: Int): String {
    delay(1000L)
    return "abc_$value"
}

In terms of threading you have nothing to worry about. Reactor is concurrency agnostic, so it can work with Kotlin coroutines threads just fine.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda