如何确定 javascript 迭代器是否提前终止?

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

假设我有一个发电机:

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

我创建可迭代对象,使用 for 循环进行迭代,然后在迭代器完全完成之前跳出循环(returns 完成)。

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

问题:如何从可迭代端发现迭代器提前终止?

如果您尝试直接在生成器中执行此操作,似乎没有任何反馈:

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

... "foo" 和 "bar" 均未记录。

编辑:请参阅较新的已接受答案。我会保留它,因为它 does/did 有效,当时我很高兴能够破解一个解决方案。但是,正如您在接受的答案中看到的那样,最终的解决方案现在已经确定了。

我注意到 typescript 将 Iterator (lib.es2015) 定义为:

interface Iterator<T> {
  next(value?: any): IteratorResult<T>;
  return?(value?: any): IteratorResult<T>;
  throw?(e?: any): IteratorResult<T>;
} 

我拦截了这些方法并记录了调用,它确实显示如果迭代器提前终止——至少通过 for-loop——然后调用 return 方法。 如果消费者抛出错误,它也会被调用。如果允许循环完全迭代迭代器 return 被调用。

Return 破解

因此,我进行了一些修改以允许捕获 另一个 可迭代对象 - 因此我不必重新实现迭代器。

function terminated(iterable, cb) {
  return {
    [Symbol.iterator]() {
      const it = iterable[Symbol.iterator]();
      it.return = function (value) {
        cb(value);
        return { done: true, value: undefined };
      }
      return it;
    }
  }
}

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

function source2(){
  return terminated(source(), () => { console.log("foo") });
}


for (let item of source2()) {
  console.log(item);
  break;
}

而且有效!

hello
foo

删除 break 你会得到:

hello
world

每次检查后 yield

在输入这个答案时,我意识到更好的 problem/solution 是在原始生成器方法中找出

我认为将信息传回原始可迭代对象的唯一方法是使用 next(value)。因此,如果我们选择一些唯一值(比如 Symbol.for("terminated"))来发出终止信号,并且我们更改上面的 return-hack 以调用 it.next(Symbol.for("terminated")):

function* source() {
  let terminated = yield "hello";
  if (terminated == Symbol.for("terminated")) {
    console.log("FooBar!");
    return;
  }
  yield "world";
}
    
function terminator(iterable) {
  return {
    [Symbol.iterator]() {
      const it = iterable[Symbol.iterator]();
      const $return = it.return;
      it.return = function (value) {
        it.next(Symbol.for("terminated"));
        return $return.call(it)
      }
      return it;
    }
  }
}

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

成功!

hello
FooBar!

链接级联Return

如果您链接一些额外的转换迭代器,那么 return 调用将通过它们全部级联:

function* chain(source) {
  for (let item of source) { yield item; }
}

for (let item of chain(chain(terminator(source())))) {
  console.log(item);
  break
}

hello
FooBar!

套餐

我已经完成了上述解决方案 as a package。它同时支持 [Symbol.iterator][Symbol.asyncIterator]。我对异步迭代器的情况特别感兴趣,尤其是当需要正确处理某些资源时。

我 运行 也有类似的需求,想弄清楚迭代器何时提前终止。接受的答案真的很聪明,可能是一般解决问题的最佳方法,但我认为这个解决方案也可能对其他用例有帮助。

例如,假设您有一个无限可迭代对象,例如 MDN's Iterators and Generators docs 中描述的斐波那契数列。

在任何类型的循环中,都需要设置条件以尽早跳出循环,就像已经给出的解决方案一样。但是,如果您想解构可迭代对象以创建一个值数组怎么办?在这种情况下,您希望限制迭代次数,本质上是在 iterable 上设置最大长度。

为此,我编写了一个名为 limitIterable 的函数,该函数将一个可迭代对象、一个迭代限制和一个在迭代器提前终止的情况下执行的可选回调函数作为参数。 return 值是使用 Immediately Invoked (Generator) Function Expression.

创建的生成器对象(既是迭代器又是可迭代对象)

当生成器执行时,无论是在 for..of 循环中,通过解构,还是通过调用 next() 方法,它都会检查 iterator.next().done === trueiterationCount < iterationLimit。在像斐波那契数列这样的无限迭代的情况下,后者总是会导致 while 循环退出。但是,请注意,也可以设置一个大于某些有限迭代长度的 iterationLimit,一切仍然有效。

在任何一种情况下,一旦 while 循环退出,将检查最近的结果以查看迭代器是否完成。如果是这样,将使用原始可迭代的 return 值。如果不是,则执行可选的回调函数并将其用作 return 值。

请注意,此代码还允许用户将值传递给 next(),这些值将依次传递给原始可迭代对象(请参阅所附代码片段中使用 MDN 的斐波那契数列的示例)。它还允许在回调函数中超出设置的 iterationLimit 对 next() 的额外调用。

运行 查看几个可能用例的结果的代码片段!这是 limitIterable 函数代码本身:

function limitIterable(iterable, iterationLimit, callback = (itCount, result, it) => undefined) {
   // callback will be executed if iterator terminates early
   if (!(Symbol.iterator in Object(iterable))) {
      throw new Error('First argument must be iterable');
   }
   if (iterationLimit < 1 || !Number.isInteger(iterationLimit)) {
      throw new Error('Second argument must be an integer greater than or equal to 1');
   }
   if (!(callback instanceof Function)) {
      throw new Error('Third argument must be a function');
   }
   return (function* () {
      const iterator = iterable[Symbol.iterator]();
      // value passed to the first invocation of next() is always ignored, so no need to pass argument to next() outside of while loop
      let result = iterator.next();
      let iterationCount = 0;
      while (!result.done && iterationCount < iterationLimit) {
         const nextArg = yield result.value;
         result = iterator.next(nextArg);
         iterationCount++;
      }
      if (result.done) {
         // iterator has been fully consumed, so result.value will be the iterator's return value (the value present alongside done: true)
         return result.value;
      } else {
         // iteration was terminated before completion (note that iterator will still accept calls to next() inside the callback function)
         return callback(iterationCount, result, iterator);
      }
   })();
}

function limitIterable(iterable, iterationLimit, callback = (itCount, result, it) => undefined) {
   // callback will be executed if iterator terminates early
   if (!(Symbol.iterator in Object(iterable))) {
      throw new Error('First argument must be iterable');
   }
   if (iterationLimit < 1 || !Number.isInteger(iterationLimit)) {
      throw new Error('Second argument must be an integer greater than or equal to 1');
   }
   if (!(callback instanceof Function)) {
      throw new Error('Third argument must be a function');
   }
   return (function* () {
      const iterator = iterable[Symbol.iterator]();
      // value passed to the first invocation of next() is always ignored, so no need to pass argument to next() outside of while loop
      let result = iterator.next();
      let iterationCount = 0;
      while (!result.done && iterationCount < iterationLimit) {
         const nextArg = yield result.value;
         result = iterator.next(nextArg);
         iterationCount++;
      }
      if (result.done) {
         // iterator has been fully consumed, so result.value will be the iterator's return value (the value present alongside done: true)
         return result.value;
      } else {
         // iteration was terminated before completion (note that iterator will still accept calls to next() inside the callback function)
         return callback(iterationCount, result, iterator);
      }
   })();
}

// EXAMPLE USAGE //
// fibonacci function from:
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Advanced_generators
function* fibonacci() {
   let fn1 = 0;
   let fn2 = 1;
   while (true) {
      let current = fn1;
      fn1 = fn2;
      fn2 = current + fn1;
      let reset = yield current;
      if (reset) {
         fn1 = 0;
         fn2 = 1;
      }
   }
}

console.log('String iterable with 26 characters terminated early after 10 iterations, destructured into an array. Callback reached.');
const itString = limitIterable('abcdefghijklmnopqrstuvwxyz', 10, () => console.log('callback: string terminated early'));
console.log([...itString]);
console.log('Array iterable with length 3 terminates before limit of 4 is reached. Callback not reached.');
const itArray = limitIterable([1,2,3], 4, () => console.log('callback: array terminated early?'));
for (const val of itArray) {
   console.log(val);
}

const fib = fibonacci();
const fibLimited = limitIterable(fibonacci(), 9, (itCount) => console.warn(`Iteration terminated early at fibLimited. ${itCount} iterations completed.`));
console.log('Fibonacci sequences are equivalent up to 9 iterations, as shown in MDN docs linked above.');
console.log('Limited fibonacci: 11 calls to next() but limited to 9 iterations; reset on 8th call')
console.log(fibLimited.next().value);
console.log(fibLimited.next().value);
console.log(fibLimited.next().value);
console.log(fibLimited.next().value);
console.log(fibLimited.next().value);
console.log(fibLimited.next().value);
console.log(fibLimited.next().value);
console.log(fibLimited.next(true).value);
console.log(fibLimited.next().value);
console.log(fibLimited.next().value);
console.log(fibLimited.next().value);
console.log('Original (infinite) fibonacci: 11 calls to next(); reset on 8th call')
console.log(fib.next().value);
console.log(fib.next().value);
console.log(fib.next().value);
console.log(fib.next().value);
console.log(fib.next().value);
console.log(fib.next().value);
console.log(fib.next().value);
console.log(fib.next(true).value);
console.log(fib.next().value);
console.log(fib.next().value);
console.log(fib.next().value);

有一种更简单的方法可以做到这一点:使用 finally 块。

function *source() {
  let i;

  try {
    for(i = 0; i < 5; i++)
      yield i;
  }
  finally {
    if(i !== 5)
      console.log('  terminated early');
  }
}

console.log('First:')

for(const val of source()) {
  console.log(`  ${val}`);
}

console.log('Second:')

for(const val of source()) {
  console.log(`  ${val}`);

  if(val > 2)
    break;
}

...产量:

First:
  0
  1
  2
  3
  4
Second:
  0
  1
  2
  3
  terminated early