今天我想知道什么是提供在打字稿一个周期,通过阵列,作为最快捷的方法:如何实现与发电机功能的循环,通过阵列
['one', 'two', 'three']
下一个值在three
将one
后,我认为这是一个很好的候选发电机功能。但它似乎并不适合我。下面的代码有什么问题?
function* stepGen(){
const steps = ['one', 'two', 'three'];
let index = 0;
if(index < steps.length - 1){
index++;
} else {
index = 0;
}
yield steps[index];
}
let gen = stepGen();
console.log(gen.next().value);
console.log(gen.next().value);
console.log(gen.next().value); // should be 'three'
console.log(gen.next().value); // should be 'one'
console.log(gen.next().value);
你需要在你的发生器代码中有一个循环。 – trincot