It starts a separate coroutine which is a lightweight thread that works concurrently with all the other coroutines. The difference is that launch returns a Job and does not carry any resulting value, while async returns a Deferred — a lightweight non-blocking future that represents a promise to provide a result later. You can use .await() on a deferred value to get its eventual result, but Deferred is also a Job, so you can cancel it if needed.
1 2 3 4 5 6 |
val time = measureTimeMillis { val one = async { FirstFunctionData() } val two = async { SecondFunctionData() } println("The answer is ${one.await() + two.await()}") } println("Completed in $time ms") |
The async/await pattern
One of the biggest problems in computing is being able to return values from asynchronous functions. Even more so, if you’re calling a function that creates a different thread to run in, you can’t return a value to the outer function. This is a program restriction because the system doesn’t know when to return, and has a hard time bridging between threads. But there is a way to achieve this behavior with the async/await pattern.
Just like you would have a Future<T>, the deferred value just wraps a potential object you can use. Once you’re ready to receive the object, you have to await for it, effectively requesting the data, which might or might not be there. If the data is already provided and delivered, the call will turn into a simple get(); otherwise, you’re code will have to suspend and wait for the data to come to the wrapper.
Run the following example:
1 2 3 4 5 6 7 8 9 10 11 12 |
public fun <T> CoroutineScope.async( context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> T ): Deferred<T> { val newContext = newCoroutineContext(context) val coroutine = if (start.isLazy) LazyDeferredCoroutine(newContext, block) else DeferredCoroutine<T>(newContext, active = true) coroutine.start(start, coroutine, block) return coroutine } |
Async-style functions
We can define async-style functions that invoke doSomethingUsefulOne and doSomethingUsefulTwo asynchronously using the async coroutine builder using a GlobalScope reference to opt-out of the structured concurrency. We name such functions with the “…Async” suffix to highlight the fact that they only start asynchronous computation and one needs to use the resulting deferred value to get the result.
1 2 3 4 5 6 7 8 9 10 |
@OptIn(DelicateCoroutinesApi::class) fun somethingUsefulOneAsync() = GlobalScope.async { doSomethingUsefulOne() } // The result type of somethingUsefulTwoAsync is Deferred<Int> @OptIn(DelicateCoroutinesApi::class) fun somethingUsefulTwoAsync() = GlobalScope.async { doSomethingUsefulTwo() } |
So, here the two coroutines are defined but not executed as in the previous example, but the control is given to the programmer on when exactly to start the execution by calling start. We first start one, then start two, and then await for the individual coroutines to finish.
Finally, we understand the difference between Async/Await in Kotlin.
Hope this blog helps you to know the difference between Async/Await in Kotlin.
So, I hope it will help you out in understanding and get a brief idea about it.
You can also check these links.
Another mentioned URL.
For more understanding please can go through this Link:
That’s all, You can enjoy your implementation.
Thank you very much.