Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!