Skip to content

Instantly share code, notes, and snippets.

@mixn
Last active April 27, 2016 12:08
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 mixn/c693f2d2d6e94526850220ac5a9e2194 to your computer and use it in GitHub Desktop.
Save mixn/c693f2d2d6e94526850220ac5a9e2194 to your computer and use it in GitHub Desktop.
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