Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support return() #22

Open
sithmel opened this issue Dec 30, 2018 · 6 comments
Open

Support return() #22

sithmel opened this issue Dec 30, 2018 · 6 comments

Comments

@sithmel
Copy link

sithmel commented Dec 30, 2018

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
Copy link
Contributor

reconbot commented Jan 2, 2019

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
Copy link
Author

sithmel commented Jan 2, 2019

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
Copy link
Contributor

reconbot commented Jan 2, 2019 via email

@sithmel
Copy link
Author

sithmel commented Jan 2, 2019

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
Copy link
Author

sithmel commented Jan 2, 2019

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

@sithmel
Copy link
Author

sithmel commented Jan 3, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants