function generators(arr) { let i = 0; return { next: function () { let none = i < arr.length; let value = none ? arr[i++] : "undefined"; return { value, none: !none } } } } var iter = generators([1, 2, 3]) console.log(iter.next()); console.log(iter.next()); console.log(iter.next()); console.log(iter.next()); console.log(iter.next());