I have method, which returns response from server. For example:
fun uploadVideo(link: String, completionHandler: (Result<String>) -> Unit) { // some action completionHandler(Result.success("")) } I want to call this method one by one. Wait for a response from the previous one to call the next one. For example
uploadVideo("https://stackoverflow.com/video1.mp4") { } // call this only when i have response from preview request uploadVideo("https://stackoverflow.com/video2.mp4") { } // call this only when i have response from preview request uploadVideo("https://stackoverflow.com/video3.mp4") { } I tried use suspendCancellableCoroutine, like this
suspend fun uploadVideo(link: String?): String? = suspendCancellableCoroutine { cont -> uri?.let { uploadVideo(link, completionHandler = { it.onSuccess { uri -> cont.resume(uri.toString()) }.onFailure { cont.resumeWithException(it) } } ) } ?: kotlin.run { cont.resume(null) } } and then call like this:
uploadVideo("https://stackoverflow.com/video1.mp4") uploadVideo("https://stackoverflow.com/video2.mp4") uploadVideo("https://stackoverflow.com/video3.mp4") but these methods are not called sequentially, but in parallel