async and await
The launch() function returns a Job object that can be used to await termination of the coroutine or to cancel. But there’s no way to return a result from the coroutine that was started using launch(). If you want to execute a task asynchronously and get the response, then use async() instead of launch().
The async() function takes the same parameters as launch(), so we can configure the context and start properties of coroutines that are created using async() and launch() in the same way. The difference, though, is that async() returns a Deferred<T> future object which has an await() method, among other methods, to check the status of the coroutine, cancel, and so on. A call to await() will block the flow of execution but not ...
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