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

Close all the arguments #2

Closed
bakkot opened this issue May 12, 2024 · 0 comments · Fixed by #4
Closed

Close all the arguments #2

bakkot opened this issue May 12, 2024 · 0 comments · Fixed by #4

Comments

@bakkot
Copy link
Collaborator

bakkot commented May 12, 2024

Consider

async function* concat(a, b) {
  yield* a;
  yield* b;
}
async function* gen(str) {
  try {
    yield str + "1";
    yield str + "2";
    yield str + "3";
  } finally {
    console.log("closed " + str);
  }
}

for await (const chunk of concat(gen("a"), gen("b"))) {
  console.log(chunk);
  if (chunk === "a2") break;
}

// oops, we never closed `b`!

(slightly simplified from here).

I think the behavior of the concat function there is not what we want. concat (or whatever equivalent we go with) is logically "taking ownership" of any iterators passed into it, which means it is responsible for closing them.

Practically, I think that means looking at each of the arguments it hasn't gotten to yet, checking to see if they have a next and return, and calling return if so. This has the downside that something with both a next and a Symbol.iterator which is not just return this will behave differently depending on when exactly concat is closed, since the approach I've outlined will check next first whereas we normally check Symbol.iterator first.

Alternatively we could do GetIteratorFlattenable and then IteratorClose, but that has the downside of opening iterators from iterables which we aren't otherwise touching. I guess the consistency argument is maybe worth that cost? Unclear.

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

Successfully merging a pull request may close this issue.

1 participant