const arr = [12, 13, 14, 15, 16]; const elem = (function *num(N) { for (var i = 0; ; i++) yield arr[i %= N]; })(arr.length); for (var i = 0; i < 17; i++) { const {value} = elem.next(); console.log(value) }