Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Does calling return on a generator call return on what it's iterating?
// test for https://mobile.twitter.com/RangerMauve/status/1276579544766578688
var fn = {
[Symbol.asyncIterator]: function(){return fn},
next: async function(){
console.log("iterating")
return { value: 42, done : false }
},
return: function(){
console.log("return")
return {
value: 92,
done: true
}
}};
var gen = async function *(fn){ for await(let i of fn){
console.log(i);
const val = yield i ; // RETURN GETS FIRED & THEN ITERATION TERMINATES HERE
console.log("post-yield", val)
await new Promise(res => setTimeout(res, 1000))
console.log("post-await")
}};
var gfn = gen(fn)
gfn.next()
gfn.next()
gfn.next()
gfn.next()
gfn.return()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment