bustle / streaming-iterables

A Swiss army knife for async iterables. Designed to replace your streams.
https://www.npmjs.com/package/streaming-iterables
MIT License
43 stars 4 forks source link

Support return() #22

Open sithmel opened 5 years ago

sithmel commented 5 years ago

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.

reconbot commented 5 years ago

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?

sithmel commented 5 years ago

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

reconbot commented 5 years ago

Yeah but eventually it will be consumed. Are you thinking about the error cases?

On Wed, Jan 2, 2019, 12:01 PM Maurizio Lupo notifications@github.com 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 .

sithmel commented 5 years ago

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.

sithmel commented 5 years ago

you can find some extra info here: http://exploringjs.com/es6/ch_iteration.html#_closing-iterators-via-return

sithmel commented 5 years ago

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.