Wraping asynchronous code with suspendCoroutine or synchronous code with withContext{}

Hi,
I’m looking for suspending way of reading/writing on file. I found this example from Kotlin/coroutines-examples which is wraping AsynchronousFileChannel with suspendCoroutine { ... }.

suspend fun AsynchronousFileChannel.aRead(buf: ByteBuffer): Int =
    suspendCoroutine { cont ->
        read(buf, 0L, Unit, object : CompletionHandler<Int, Unit> {
            override fun completed(bytesRead: Int, attachment: Unit) {
                cont.resume(bytesRead)
            }

            override fun failed(exception: Throwable, attachment: Unit) {
                cont.resumeWithException(exception)
            }
        })
    }

And I’m wondering if there is any benefit from wraping synchronous call with withContext{} like so
val text = withContext(IO) { File(...).bufferedReader().readText() }

Hi @stasbar,
aRead does not require a specific Dispatcher, so this example is OK.

You may be interested to Asynk-nio library

So the first approach creates new thread for each new call ? Could you proof that ? My diving down starts here and ends up here. Documentation says that the channel is “is associated with default thread pool to which tasks are submitted to handle I/O events”, which doesn’t fulfill my curiosity