今天我想知道什麼是提供在打字稿一個週期,通過陣列,作爲最快捷的方法:如何實現與發電機功能的循環,通過陣列
['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