Async_chan.send(msg); <-- ever a good idea?

Is there ever a situation where

async_chan.send(msg); makes sense, or should it always be

async_chan.send(msg).await; or

let x = async_chan.send(msg); ?

This really depends if send() may need to wait or not. A bounded channel may need to wait for space, but an unbounded channel can grow, so its send() is likely not an async method.

If send() is itself awaitable, you should of course await on it otherwise it might not do anything at all (due to being "lazy" until polled). There might also be an alternate method like try_send() that returns an error if waiting would be needed.

1 Like

Yeah, if the send method can always complete immediately, it doesn't need to be async. For example, Tokio's bounded and unbounded channels illustrate this by how the bounded channel has an async send, but the unbounded channel has a non-async send.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.