Last active
June 26, 2020 18:35
-
-
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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