Could someone explain to me what's going on here? ...
# coroutines
g
Could someone explain to me what's going on here? I get errors on the suspending calls? Is that due to the sequence or why would I lose my coroutine-context from the "suspend" function?
Copy code
private suspend fun processAndSend() {
    documentService.getDocuments(reference) // returns a Sequence<Documents>
            .chunked(10)
            .map { chunkedDocuments ->
                val processedDocuments = httpClient.processDocuments(chunkedDocuments) // compile error, suspending call
                queueSender.send(processedDocuments) // compile error, suspending call
            }
}

// compile error = Suspension functions can be called only within coroutine body
a
The sequence scope is restricted, you can’t call suspend functions that are not extensions of SequenceScope. It looks like you want to use
.asFlow()
before
map
to open up calling arbitrary suspend functions
g
@araqnid aha so when I call the sequence, that will create a restricted coroutineScope to allow to pickup the continuation on the
yield
?
ah, just realized I could even remove the suspend from my sequence function
suspend fun getDocuments()
that makes sense then.. I guess I could rewrite the sequence to a flow if I want a non-blocking behavior then?
a
right, the body of
sequence{}
is only allowed to suspend by calling
yield
(ultimately, maybe via
yieldAll
or some other extension of
SequenceScope
). This is totally independent of things like
coroutineScope
from the kotlinx.coroutines library
g
gotcha, that was where my confusion came from. Thanks!
a
Re-reading, I think I answered the wrong question; and that the right answer is that the block for Sequence.map isn’t inlined, and in fact can’t suspend at all 🙈 Either way, yes, I think a Flow is the way to go if you want to interact with other suspending functions in the sequence processing