I need to make api call before until the server sends a success status. The request must be executed with a delay of two seconds, and all this must be done until 10 seconds have elapsed.
Here are my attempts:
suspend fun loadData(): Data {
try {
withTimeout(10000) {
val data = repository.getData()
if (data.status == "SUCCESS") {
// complete
} else {
delay(1000)
// and after delay I need again call repository.getData(), until the status is SUCCESS
}
}
} catch (e: Exception) {
// complete
}
}
But I do not understand how to loop the execution of the request and it seems to me that something is wrong with my code. Perhaps there is a more concise way, please help me.
I believe you can use a simple
whileloop for that: