I highly recommend to consider a different signature for getData function that does not expose Deferred (future) type, but is built with suspend fun instead. Something like:

suspend fun getData(provider: () -> Data): Data

You can use it with the recent version of Retrofit that supports suspending functions like this:

suspend fun getData(): Data = 
cacheProviders.getData { retrofitService.getData() }

It is less error-prone, reduces the risk of accidentally leaking resources or loosing errors. It is also more convenient, since the consuming code does not have to use .await() .

Written by

Project Lead for the Kotlin Programming Language @JetBrains

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store