Skip to content

Instantly share code, notes, and snippets.

@qubyte
Last active January 2, 2016 03:29
Show Gist options
  • Save qubyte/8243729 to your computer and use it in GitHub Desktop.
Save qubyte/8243729 to your computer and use it in GitHub Desktop.
Demonstrates the avoidance of a closure for the trivial task of getting a time difference from invocation using a generator.
var dt = (function () {
var start = Date.now();
return function () {
return Date.now() - start;
};
})();
// Some time later...
console.log(dt())
var dt = (() => {
let gen = (function* () {
let start = Date.now();
while (true) {
yield Date.now() - start;
}
})();
gen.next(); // Start the clock...
return gen;
})();
// Some time later...
console.log(dt.next().value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment