What's the difference between doing these two solutions to the same problem?
Closure Method
const numberIncrementer = startValue => () => startValue++ const getNextNumber = numberIncrementer(0) console.log(getNextNumber()) // 0 console.log(getNextNumber()) // 1 console.log(getNextNumber()) // 2 Generator Method
const numberIncrementer = function*(startValue) { while(true) { yield startValue++ } } const numberFactory = numberIncrementer(0) const getNextNumber = () => numberFactory.next().value console.log(getNextNumber()) // 0 console.log(getNextNumber()) // 1 console.log(getNextNumber()) // 2 Viewing these two methods, what reason do I have to choose one over the other?
.next()method hard coded into yourgetNextNumberfunction, it would perhaps more appropriately be outside of that function so you can callnext()after someasyncwork returns with data. But i could be wrong about all of this...Iterators, so you can usefor..ofto iterate over the data a generator produces. Another difference as that with the Generator you could process an infinite sequence.