Last active
April 27, 2016 12:08
-
-
Save mixn/c693f2d2d6e94526850220ac5a9e2194 to your computer and use it in GitHub Desktop.
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
function readLinesSync (fileName) { | |
// Dummy | |
const file = { | |
fileName, | |
isAtEndOfFile: () => false, | |
close: () => 'Closed' | |
}; | |
return { | |
[Symbol.iterator] () { | |
return this; | |
}, | |
next () { | |
if (file.isAtEndOfFile()) { | |
file.close(); | |
return { done: true }; | |
} else { | |
return { value: 'foo' }; | |
} | |
}, | |
// Seems to never get called? | |
return () { | |
console.log(file.close()); // Never gets logged | |
return { done: true }; | |
} | |
}; | |
} | |
for (const line of readLinesSync('foo.txt')) { | |
console.log(line); // 'foo' | |
break; // Shouldn’t this automatically call the iterator’s `return` method and log 'Closed'? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment