Hi all,

```groovy
def channel = AsyncChannel.create(2)

// send data to channel in another thread
async {
    for (int i = 1; i <= 3; i++) {
        someCall()
        channel.send(i * i)
    }
    channel.close()
}

// print all values send till the channel closed
for await (value in channel) {
    println value
}
```

This code has two problems is `someCall()` throws an exception:
* `channel.close()` is never called
* the exception caused by `someCall()` is swallowed and it is left to the global async exception handler to do something with it. The fix looks ugly:

```groovy
def channel = AsyncChannel.create(2)

// send data to channel in another thread
def promise = async {
    try {
        for (int i = 1; i <= 3; i++) {
            someCall()
            channel.send(i * i)
        }
    } finally {
        // make sure the channel still closes in case of an exception
        channel.close()
    }
}

// print all values send till the channel closed
for await (value in channel) {
    println value
}

// throw exception if there was any
await promise
```

`for await` cannot solve the problem here, because `promise` carries the exception, not `channel`. `async {...} finally {...}` could work, but I hope somebody has a better suggestion. Especially since the example here has a shared construct, but that may not always be the case. And then this would mean to have to move things, that could stay in the async, to outside of the async. So I am not really happy with that idea

bye Jochen

Reply via email to