Support return()
If you are not using the "for await of" loop you need to manually call the method return() when the loop is interrupted.
See my notes here: https://github.com/sithmel/iter-tools/wiki/Generator-functions-work-flow
Also there are plenty of examples in the iter-tools code base.
Interesting, the only utility I've got that might not consume an iterable in it's entirety is take(). I think take() should probably be able to accept an iterator instead of just an iterable. But even in that case ... I'm not sure I want to call return on it. Any particular situations we might want to call return() on?
If you consume an async iterable like this: https://github.com/bustle/streaming-iterables/blob/master/lib/parallel-map.ts#L28
you never fire "return" on the consumed iterable
Yeah but eventually it will be consumed. Are you thinking about the error cases?
On Wed, Jan 2, 2019, 12:01 PM Maurizio Lupo [email protected] wrote:
If you consume an async iterable like this: https://github.com/bustle/streaming-iterables/blob/master/lib/parallel-map.ts#L28
you never fire "return" on the consumed iterable
— You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/bustle/streaming-iterables/issues/22#issuecomment-450920906, or mute the thread https://github.com/notifications/unsubscribe-auth/AABlbkiXXvU451AVcctRxmIvylvyKnvQks5u_OYCgaJpZM4Zk8nr .
The error case is one. You have also this case: https://github.com/bustle/streaming-iterables/blob/master/lib/merge.ts#L8 If the consumer doesn't finish to consume this iterator, the iterable in input is left unclosed.
you can find some extra info here: http://exploringjs.com/es6/ch_iteration.html#_closing-iterators-via-return
Example:
async function example() {
const mergedIterable = merge(iterable1, iterable2, iterable3)
for await (const item of mergedIterable) {
if (item === x) {
console.log('Found x')
break // this fires the method "return" on mergedIterable
}
}
}
iterable1, iterable2 and iterable3 are left unfinished and they are never closed. I experimented using "finally" in generator functions. This is executed when the method "return" is called.