Does anyone have any solutions for returning a res...
# coroutines
j
Does anyone have any solutions for returning a result to a
Channel
sender? I'm using a rendezvous
Channel
too synchronously send data to consumer coroutine that writes to a output stream. I would like to be able to return a
boolean
value indicating the result of the write to the coroutine that handles sending the work to the rendezvous
Channel
.
j
I don't understand your setup. Since there is no buffering, why not perform the write to the output stream directly from the same coroutine?
j
The way my code is written, I have a dedicated coroutine that accepts my
OutputStream
as an argument and will loop indefinitely writing data when it arrives in the
Channel
. I opted to use a channel so that I could eliminate the need for null checks on the stream, since my coroutine received a non-null instance.... I also thought it was best to use a channel to safely communicate between two coroutines... With that being said I'm not entirely sure I need to have a dedicated output coroutine.
j
Send a callback
Send a completable deferred
2
👍🏿 1
y
A technique in concurrent programming is to send a reply channel that you want the person to reply on. Sending a completable deferred is definitely the better way since it enforces a single reply!
K 1
j
So yeah, we can replace the return channel with a deferred, and then go one step further and replace the send channel with a suspend call. No need for a channel or multiple coroutines at all in this case.
1
It would only be useful if there was a buffer, but we're talking about a rendezvous channel here.
j
Is the deferred necessary if I use a suspend function instead? The function could return a value indicating the result.
Also, what's the intended use case of a rendezvous
Channel
if it shouldn't be used as a mechanism for communicating between two Coroutines?
j
It's unidirectional, you wanted bidirectional
j
Ah, I see. If I didn't want to signal success to sender, it would be fine to use..👌🏿
j
Also, I didn't say it shouldn't be used as a mechanism to communicate between coroutines. I'm saying you probably don't need 2 coroutines in the first place in your case, which simplifies the problem of 2-way communication because it removes the need for a channel in the first place