Skip to content

Instantly share code, notes, and snippets.

@py-in-the-sky
py-in-the-sky / recursive_generator_function.js
Last active August 29, 2015 14:28
Recursive Generator Function in JavaScript
// Works in Chrome devtools console
function* downToZero (n) {
yield n; // yield "first" element of the "list"
if (n > 0) // yield each element of the "rest" of the "list"
for (var n2 of downToZero(n - 1))
yield n2;
}
var tenToZero = downToZero(10);