Skip to content

Instantly share code, notes, and snippets.

@rektide
Last active June 26, 2020 18:35
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 rektide/fc3a635cf0b783839935872995253bf7 to your computer and use it in GitHub Desktop.
Save rektide/fc3a635cf0b783839935872995253bf7 to your computer and use it in GitHub Desktop.
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