Coroutine GlobalScope.async vs GlobalScope.launch

Hi,
I’m learning Kotlin Coroutines. Code below shows time measure between GlobalScope.async vs GlobalScope.launch. The problem is that result is same despite that first part is executed asynchronously.

 fun main(args: Array<String>) {
     runBlocking {
         println("aSync GlobalScope")
        var time = measureTimeMillis {
            val a1 = GlobalScope.async {
                delayOnSecond(1000)
                delayOnSecond(500)
            }
            val a2  = GlobalScope.async {
                delay(1000)
                delay(500)
            }
            a1.await()
            a2.await()
        }
        println("Total time: $time")
        println("sequential GlobalScope")

         time = measureTimeMillis {
             val a1 = GlobalScope.launch {
                delay(1000)
                delay(500)
            }
             val a2 = GlobalScope.launch {
                delay(1000)
                delay(500)
            }
            a1.join()
            a2.join()
        }
        println("Total time: $time")

    }

}

Shouldn’t be in second example 3000ms rather than 1500?

1 Like

They both execute asynchronously. The difference between async and launch is that async returns a value and launch doesn’t.

2 Likes

I thought launch returns the job, which is a lifecycle for the scope?, so you can cancel and other things. So, it has a return, but it is not the results (learning).

This is referring to the suspend lambda passed in, not the launch or async method itself. launch returns Job. async returns Deferred (which extends Job) which also has methods for getting the suspend lambda’s result.

Thanks @nickallendev that makes sense now; However, unsure how anyone would know that though from just that text without the qualification.