function* sum(array) { var sum = 0; for (var x of array) { yield sum += x; } } for (let x of sum([1, 2, 3, 4])) { console.log(x); }