https://kotlinlang.org logo
Title
m

marcelo

11/05/2018, 2:20 PM
unless I have to do this:
return templates.map { renderTemplatePath -> 
  runBlocking {
    val result = async {
      renderMetricTimer.tagVal(package).time {
        //code
      }
    }
    result.await()
 }.toSet()
}
Obviously, I am new to coroutines, but this seems like a lot of coroutine builders, no?
s

spand

11/05/2018, 2:21 PM
Why do you run something in async if you await it immediately after?
m

marcelo

11/05/2018, 2:22 PM
the
timer
function is a
suspend
function.
suspend fun <T> NeedsTag1<BooleanTagValue, Timer>.time(toTime: suspend () -> T): T {
    val timerStarted = this.start()
    try {
        val result = toTime()
        timerStarted.tagVal(true).stop()
        return result
    } catch (e: Throwable) {
        timerStarted.tagVal(false).stop()
        throw e
    }
}`
s

spand

11/05/2018, 2:22 PM
You can call suspend functions from inside runBlocking
m

marcelo

11/05/2018, 2:23 PM
OH, and avoid the
async
??
s

spand

11/05/2018, 2:23 PM
Yes. Async is for parallel work only
👍 1
m

marcelo

11/05/2018, 2:24 PM
I'm still confuse on those. I thought that would use
async
when I need to return something.
n

nulldev

11/05/2018, 3:36 PM
Nah, you can just do:
runBlocking {
    renderMetricTimer.tagVal(package).time {
        //code
    }
}.toSet()
👍 1