[英]How do you terminate an 'infinite' generator in JavaScript?
EDIT: I edited to include more actual code it seems my 'slimmed down' example was a bit confusing.编辑:我进行了编辑以包含更多实际代码,看来我的“精简”示例有点令人困惑。
I have a javascript array, questions
of unknown length which contains objects, one for each 'question', each of which has the shape我有一个 javascript 数组,包含对象的未知长度的
questions
,每个“问题”一个,每个问题都有形状
{
questionID: 1,
questionText:'bla bla bla',
end: false
}
I also have a function, getNextQuestion
that takes an id
and returns a screen我还有一个 function,
getNextQuestion
,它接受一个id
并返回一个屏幕
const getNextQuestion = id => R.find(R.propEq(['questionID'], id))(questions);
(one could of course use Array.find
for the above, but I'm exploring Ramda) (当然可以使用
Array.find
进行上述操作,但我正在探索 Ramda)
I want to write a generator that will yield a particular object from this array, chosen by a passed in id
, up until an object is yielded which has end === true
我想编写一个生成器,它将从此数组中产生一个特定的 object,由传入的
id
选择,直到产生一个具有end === true
的 object
That is, the object with this particular id
should be the last value returned from the generator.也就是说,具有此特定
id
的 object 应该是生成器返回的最后一个值。 After that the generator should yield {value: undefined, done: true
之后生成器应该产生
{value: undefined, done: true
I thought to write something like this我想写这样的东西
export function* questionGenerator() {
let nextID = 1;
let continueLoop = true;
while (continueLoop) {
const question = getNextQuestion(nextID);
if (question.end) continueLoop = false;
nextID = yield getNextQuestion(nextID);
}
}
which would be called like this会这样称呼
const qGen = questionGenerator();
const id = 1;
do {
const result = gGen.next(i++)
console.log(result.value)
} while (!result.done)
but I'm not sure if that's the best way to write it.但我不确定这是否是最好的写法。
Is there a better way?有没有更好的办法?
You could return
from the generator, which will simplify your code:您可以从生成器
return
,这将简化您的代码:
while (true) {
const screen = getNextScreen(nextID);
nextID = yield screen;
if (screen.end) return;
}
You need some changes in the generator to return the generator and another change in the loop for the call of the generator.您需要在生成器中进行一些更改以返回生成器,并在循环中进行另一项更改以调用生成器。
const questions = [{ questionID: 1, questionText:'bla bla bla', end: false }, { questionID: 2, questionText:'bla bla bla', end: false }, { questionID: 3, questionText:'bla bla bla', end: false }, { questionID: 4, questionText:'bla bla bla', end: false }, { questionID: 5, questionText:'bla bla bla', end: true }]; const getNextQuestion = (array => id => array.find(({ questionID }) => questionID === id))(questions); function* questionGenerator() { let nextID = 1; while (true) { const question = getNextQuestion(nextID); nextID = yield question; if (question.end) return; } } const gGen = questionGenerator(); var id = 1; while (true) { const result = gGen.next(id++); if (result.done) break; console.log(result.value); }
.as-console-wrapper { max-height: 100%;important: top; 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.