Coroutines: Jobs

Where do they come from?

Whenever the launch builder is used to create a coroutine, the builder returns an instance of Job. This instance will give us finer control over the lifecycle of the coroutine.

Cancellation

One use case for the Job is to cancel the coroutine at some point in its run time. This could be to stop a progress bar or download from continuing.

This can be achieved by using job.cancel()

Join

Coroutines are able to wait for others to finish by calling the job.join() function. This is helpful when you have coroutines that are dependent on others to finish before running the next block of code. An example where this is useful is at the end of a successful purchase of a product. Your app would have to wait for the response of the purchase network request before continuing down the happy path or the sad path.