Roman Elizarov
1 min readMar 27, 2019

--

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() .

--

--

Roman Elizarov
Roman Elizarov

Written by Roman Elizarov

Project Lead for the Kotlin Programming Language @JetBrains

No responses yet