Monday, 12 April 2021

How can I find out if a javascript iterator terminates early?

Lets say I have a generator:

function* source() {
  yield "hello"; yield "world";
}

I create the iterable, iterate with a for-loop, and then break out of the loop before the iterator fully completes (returns done).

function run() {
  for (let item of source()) {
    console.log(item);
    break;
  }
}

Question: How can I find out, from the iterable-side, that the iterator terminated early?

There doesn't seem to be any feedback if you try to do this directly in the generator itself:

function* source2() {
  try {
    let result = yield "hello";
    console.log("foo");
  } catch (err) {
    console.log("bar");
  }
}

... neither "foo" nor "bar" is logged.



from How can I find out if a javascript iterator terminates early?

No comments:

Post a Comment