Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created May 7, 2014 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdickinson/3e9baccfae6b77fc27a5 to your computer and use it in GitHub Desktop.
Save chrisdickinson/3e9baccfae6b77fc27a5 to your computer and use it in GitHub Desktop.
function *gen() {
for(var i = 0, len = 10; i < len; ++i) {
try {
console.log('try')
console.log('got try', yield i)
} catch(err) {
console.log('catch')
console.log('got catch', yield i + 10)
} finally {
console.log('finally')
console.log('got finally', yield i + 20)
}
}
}
var iter = gen()
, accum = 0
, i = 2
, value
do {
console.log('---- ' + i +' -----')
value = i % 3 === 0 ?
iter.throw(new Error('throw')) : iter.next('recv')
if(value.value !== undefined) {
accum += value.value
}
++i
} while(!value.done)
// WHAT IS ACCUM?
// how many times did it iterate?
console.log(accum, i - 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment