Skip to content

Kotlin Coroutines join vs await

Devrath edited this page Jan 16, 2024 · 3 revisions

kotlin_join_await

Join()

  • 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.

Best practices for using join()

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().

Await()

  • Using await, We can return the result of the suspending function which is not possible using Join()

Best practices for using Await

Use await, when you want multiple blocks of code to execute parallel and return the result for them

Kotlin coroutine join() vs await

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.
Clone this wiki locally