Returning results
Calling launch() is much like calling a function that returns Unit. But most of our functions return some kind of result. For that purpose, we have the async() function. It also launches a coroutine, but instead of returning a job, it returns Deferred<T>, where T is the type you expect to get later.
Think of a situation where you would like to fetch the user's profile from one source and their history from another. It may be two DB queries, or a network call to two remote services, or any combination.
You must show both the profile and the history, but you don't know which returns first. Usually, retrieving the profile is faster. But sometimes there may be a delay, since profiles are updated often and the history will return ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access