July 2018
Intermediate to advanced
266 pages
7h 11m
English
The obvious solution to this issue is not to assume that a value is ready, but to always wait for it to be. In this case, we simply need to change our implementation to stop using getCompleted() and use await() for all the jobs to complete:
suspend fun getUser(id: Int): User { val name = datasource.getNameAsync(id) val age = datasource.getAgeAsync(id) val profession = datasource.getProfessionAsync(id) // Wait for each of them, don't assume they are ready return User( name.await(), age.await(), profession.await() )}
Read now
Unlock full access