-
Notifications
You must be signed in to change notification settings - Fork 22
Kotlin Coroutines join vs await
Devrath edited this page Jan 16, 2024
·
3 revisions
- It helps to make the control flow of a co-routine wait until the coroutine is finished
- But It does this without blocking the main thread.
- It is important for writing efficient and responsive applications.
Use join() sparingly. Only use join() when you need to wait for the completion of a coroutine. If you donβt need to wait for the completion of a coroutine, you should avoid using join().
- Using await, We can return the result of the suspending function which is not possible using Join()
Use await, when you want multiple blocks of code to execute parallel and return the result for them
The join() and await() functions are both suspending functions that allow you to wait for the completion of a coroutine before continuing. However, there are some key differences between the two functions.
- The join() function suspends the current coroutine until the coroutine associated with the Job object completes. The Job object is returned by the launch() and async() functions when you start a coroutine.
- The await() function suspends the current coroutine until the coroutine associated with the Deferred object completes. The Deferred object is returned by the async() function when you start a coroutine that returns a value.