I'm writing an Android app for the first time, using Kotlin in Android Studio. I am quite new to asynchronous programming.
The app will display some data to the user which it will obtain from a REST API using Volley. To get a meaningful set of data to be displayed all together, the responses from three GET requests are needed.
The first GET request doesn't require the output from the other two GET requests in order to set its query parameters.
The second and third GET requests both require the output from the first GET request in order to set their query parameters, but they are not dependent on the output of each other so it doesn't matter which of these two is called or returns first.
Once all three requests have returned responses, the results are assembled into some meaningful information which is displayed to the user.
The way I have implemented this is to have the first GET request be called from my top-level routine, and then to have the second and third requests called sequentially from inside the response listener function of the first request. A final function which assembles the information and displays it to user is called from both the response listener functions of the second and third requests; this final function first increments a counter by one, and then only carries out the rest of its work if the counter has reached a value of 2, meaning that the work will only be carried out when the function is called by the second of the latter two requests to return.
I feel like it would be "better" (in a way I don't quite know how to describe) if I could code this in such a way that all three Volley GET requests could be called sequentially from my top-level routine, followed by the assembly and display of data to the user also being done sequentially following the calling of these GET requests in my top-level routine.
This seems like it would require the calls to the second and third GET requests not to "fire" until the first request has returned, and for the assembly & display code not to "fire" until all three requests have returned. But I don't know how to express this in Kotlin.
Is there a nice, neat, standard way for me to write this code sequentially, or should I stop thinking that writing this sequentially would be better just for the sake of it?