good day everyone. got a coroutine question. I wan...
# getting-started
o
good day everyone. got a coroutine question. I want to set the property value for each object in the list by calling the suspension function and setting the result:
Copy code
coroutineScope {
            workflows.forEach {
                it.status = formWorkflowActivationStateManager.getCurrentState(it, companyId).toWorkflowStatus()
            }
        }
I’m wondering if the
coroutineScope
is enough to parallel those executions or if I need to use
async/await
?
s
Yes, you need to use
async
— coroutines are sequential by default
o
thanks @Sam but could you give me a hint on how to implement that for each entity in a list?
s
Wrong Sam 😉
👋 1
s
In your example, since you don’t need to return a result from the parallel tasks, you can just use
launch
, like:
Copy code
coroutineScope {
    workflows.forEach {
        launch { it.status = ... }
    }
}
The
coroutineScope
will wait for all the launched tasks to finish before returning.
👍 2
o
sorry @Sam my bad 😄
💖 1
and
launch
will create a parallel task for each entity, right?
s
Yes 👍
y
Indeed it will. The equivalent if you wanted a result out of each would be to do
Copy code
coroutineScope {
  workflows.map { async { it.status = ... } }.awaitAll()
}
Keep in mind that this and
launch
will cancel all operations eventually if one fails. If that's not what you want, you can do
supervisorScope
instead of
coroutineScope
o
prefect, thanks everyone for help!