What’s the correct way to keep receiving values from an unbuffered channel until it’s closed?
Depends on what you’d like to do with those values. If the code that sends the values to the channel is expected to close it after it’s done producing them, you could do something like this:
channel = Channel(MyThing).new
spawn do
loop do
if thing = channel.receive?
process thing
else
break
end
end
end
do_something_that_sends_things_to(channel)
Channel#receive?
will return nil
if the channel closes before it receives the value.
1 Like
Thank you. That’s exactly what I was looking for.
1 Like