brentburgoyne/chan — pull request #6 by brentburgoyne (2013-11-30 17:38:45+00:00)
Title: Added close and done methods to the channel with a close example.

I am putting this in as a pull request so anyone who is willing to join in on the discussion can weigh in on how this might be implemented.

Go's channels, which chan is loosely based on, can be closed. This branch is an attempt to add similar functionality to chan. There are now two methods added to the channel.

```javascript
ch.close(); // This will prevent values from being added to the channel
ch.done(); // Returns true only if the channel is closed and empty
```

The problem that I am dealing with is how to handle pending callbacks/yields when the channel is closed. This is how it works in Go:

> After the last value has been received from a closed channel c, any receive from c will succeed without blocking, returning the zero value for the channel element. The form `x, ok := <-c` will also set ok to false for a closed channel.

Since Go is statically typed, the values for a channel must be of the type declared when creating the channel. The channel returns the zero value for that type. Go also has multiple return values. The channel returns a second boolean value indicating if the value was a zero value due to the channel being closed.

As of right now the pending yields return `undefined`, but I am not sure that is the right decision. The other option would be to return an `Error` that would be thrown in the `co` instance and handle this with a `try/catch`.

Does anyone have an opinion of what would make more sense or other ideas?