I am trying to run a function based on the response data of a repo call, and running into an issue with race conditions / returning data using coroutine scope. Based on the two pseudo code blocks, I was wondering if I could get some help?
Option 1: Can't return a response within a coroutine scope without using runBlocking.
fun mainFunction(): Boolean {
return subFunction(getResponse()) //returns boolean
}
private fun getResponse () {
scope.launch{
val response = async { someApiCall }.await()
return response
}
}
Option 2: The response value is not initialized by the time the subFunction is called, thus leading to an error.
lateinit var response: MutableList<>
fun mainFunction(): Boolean {
return subFunction(response) //returns boolean
}
private fun getResponse () {
scope.launch{
response = async { someApiCall }.await()
}
}