Did kotlin allows suspend functions to be called f...
# coroutines
t
Did kotlin allows suspend functions to be called from non-suspend functions or without coroutine scope in an older version? The code snippet below shouldn't compile because
getUser
function is not suspend, but it is calling
refreshUser
function. The code is from here https://developer.android.com/jetpack/guide#persist-data
fun getUser(userId: String): Flow<User> {
refreshUser(userId)
// Returns a Flow object directly from the database.
return userDao.load(userId)
}
private suspend fun refreshUser(userId: String) {
// Check if user data was fetched recently.
val userExists = userDao.hasUser(FRESH_TIMEOUT)
if (!userExists) {
// Refreshes the data.
val response = webservice.getUser(userId)
// Check for errors here.
`// Updates the database. Since
userDao.load()
returns an object of` `//
Flow<User>
, a new
User
object is emitted every time there's a` `// change in the
User
table.`
userDao.save(response.body()!!)
}
}
j
I don't think this has ever been allowed. I imagine there must be a mistake in the example code
2
t
Ah kk thanks for your input, the earliest doc I found on coroutines is back in Jan 31, 2017 https://github.com/Kotlin/kotlinx.coroutines/blob/0.5-beta/coroutines-guide.md I just didn't think Google would post code that wouldn't even compile, so I thought I missed something.
a
Nah, happens all the time through "refactoring" the code in the CMS instead of with other tooling available. There should be a, "report issue" somewhere on the page
😆 1
This example probably used to have refreshUser just kick off an async request and someone said it should suspend using structured concurrency principles instead