# Reading values from channel

**URL:** <https://forum.crystal-lang.org/t/reading-values-from-channel/2375>\
**Category:** Help & Support\
**Created:** [July 25, 2020, 10:32pm UTC](https://forum.crystal-lang.org/t/reading-values-from-channel/2375 "2020-07-25T22:32:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![imeraj](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/imeraj/32/876_2.png) [@imeraj](https://forum.crystal-lang.org/u/imeraj)\
**Post date:** [July 25, 2020, 10:32pm UTC](https://forum.crystal-lang.org/t/reading-values-from-channel/2375/1 "2020-07-25T22:32:41Z")

</div>

What’s the correct way to keep receiving values from an unbuffered channel until it’s closed?

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [July 25, 2020, 11:34pm UTC](https://forum.crystal-lang.org/t/reading-values-from-channel/2375/2 "2020-07-25T23:34:02Z")

</div>

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:

```crystal
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.

---

<div class="post-metadata">

**Author:** ![imeraj](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/imeraj/32/876_2.png) [@imeraj](https://forum.crystal-lang.org/u/imeraj)\
**Post date:** [July 25, 2020, 11:58pm UTC](https://forum.crystal-lang.org/t/reading-values-from-channel/2375/3 "2020-07-25T23:58:43Z")

</div>

Thank you. That’s exactly what I was looking for.
