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

171
Vistas
Launching coroutines from a suspended function

Given we have a suspending function but this is not a CoroutineScope, how can we launch other coroutines such that those are associated with the current scope of whatever runs this suspending function?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Every suspendable function has access to the global variable coroutineContext which you can trivially wrap in CoroutineScope, but that is not its intended purpose. It's there so you can check at any point whether your coroutine was cancelled, reach the debug info like the job name, etc.

In the words of Roman Elizarov in his recent Medium post:

suspend fun doNotDoThis() {
    CoroutineScope(coroutineContext).launch {
        println("I'm confused")
    }
}

Do not do this!

A suspendable function should not fire off concurrent work that may go on after it returns. It should only use concurrency to achieve parallel decomposition of a task, and that means it will wait for all the child coroutines to complete.

You should decide to either use a plain function that is the receiver of CoroutineScope (signaling the intention to launch concurrent work) or use a suspendable function that awaits on the completion of all the work it initiated.

So, if you want parallel decomposition, then use a coroutineScope or, possibly a supervisorScope block:

coroutineScope {
    launch { 
        // ... task to run in the background
    }
    // ... more work while the launched task runs in parallel
}
// All work done by the time we reach this line

coroutineScope is a suspendable function and it won't complete until all the coroutines it launched complete.

over 4 years ago · Santiago Trujillo Denunciar

0

You can create an extension function on CoroutineScope or function with CoroutineScope as a parameter:

fun CoroutineScope.doThis() {
    launch { ... }
}

fun doThatIn(scope: CoroutineScope) {
    scope.launch { ... }
}

Also you can use coroutineScope or supervisorScope, depending on your needs:

suspend fun someFun() = coroutineScope {
    launch { ... }
}

suspend fun someFun() = supervisorScope {
    launch { ... }
}
over 4 years ago · Santiago Trujillo Denunciar

0

You could just use withContext() or coroutineScope() for launching another coroutine:

withContext(coroutineContext) {
    launch { ... }
}

While the second would override the Job of the context, but reuse the context:

coroutineScope {
    launch { ... }
}
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