kotlin / com.liulishuo.okdownload.kotlin / com.liulishuo.okdownload.DownloadTask / await

await

suspend fun DownloadTask.await(block: () -> Unit = {}): DownloadResult

Awaits for completion of the DownloadTask without blocking current thread. Returns DownloadResult or throws corresponding exception if there is any error occurred.

This suspending function is cancellable. If the Job of the current coroutine is cancelled or completed while this suspending function is waiting, this function immediately resumes with CancellationException.

Parameters

block -

does something after the DownloadTask start immediately.

Show progress using channel after await, for example:

val task = DownloadTask.Builder().build()
GlobalScope.lunch {
    val downloadResult = task.await {
        val progressChannel = task.spChannel()
        // do progress showing with progressChannel
    }
}