Currently, I am making API calls to 2 to 3 different APIs, where the second API Call relies on data from the first. However, the compiler calls the 2nd function even before the first function is completed, causing an error. How can I call the 2nd function only after the first is done? Thank you
/**
* Function to get Bus Timings depending on Bus Service No. or Bus Stop Code
*/
fun determineUserQuery(userInput: String) {
// Determine if User Provided a Bus Service No. or Bus Stop Code
val userInputResult = determineBusServiceorStop(userInput)
viewModelScope.launch {
if (userInputResult.busServiceBool) {
busServiceBoolUiState = true
coroutineScope {
// Provided Bus Service, Need get Route first
getBusRoutes(targetBusService = userInputResult.busServiceNo)
}
delay(2000)
// Get the Bus Timing for Each Route
Log.d("debug2", "String ${_busRouteUiState.value.busRouteArray}")
getMultipleBusTimings(busRoutes = _busRouteUiState.value.busRouteArray)
}
else {
// Provided Bus Stop Code
coroutineScope {
launch {
getBusStopNames(targetBusStopCode = userInputResult.busStopCode?.toInt())
}
launch {
getBusTimings(userInput = userInputResult.busStopCode)
}
}
}
}
}