Playing with generator functions
by Arnaud Buchholz
JavaScript
function * toto () {
yield 'toto'
return 123
}
function * test1 () {
yield 'start'
yield * toto()
yield 'end'
}
function * test2 () {
yield 'start'
return 'end'
}
function protect (callback) {
try {
return callback()
} catch (e) {
return {}
}
}
function display (test) {
// console.log([...test()])
const iterator = test.call(null)
while (true) {
const { value, done } = protect(() => iterator.next())
console.log(value)
if (done) {
break
}
}
}
console.log('--- test1 ---')
display(test1)
// console.log('--- test2 ---')
// display(test2)