Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda