In the kotlinx.coroutines
library you can start new coroutine using either launch
(with join
) or async
(with await<
Async vs Launch Async vs Launch Diff Image
launch / async no result
async for result
launch returns a job
async returns a result (deferred job)
launch with join is used to wait until the job gets finished.it simply suspends the coroutine calling join(), leaving the current thread free to do other work (like executing another coroutine) in the meantime.
async is used to compute some results. It creates a coroutine and returns its future result as an implementation of Deferred. The running coroutine is canceled when the resulting deferred is canceled.
Consider an async method that returns a string value. If the async method is used without await it will return a Deferred string but if await is used you will get a string as the result
The key difference between async and launch. Deferred returns a particular value of type T after your Coroutine finishes executing, whereas Job doesn’t.