Last active
April 7, 2019 17:09
-
-
Save ukoloff/d27e6d99b437ade49cadace58dbe7ea4 to your computer and use it in GitHub Desktop.
Hand made JS generators
This file contains 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 range(from, to) { | |
return { next: nextPromise, [Symbol.asyncIterator]: iterator } | |
function next() { | |
return from <= to | |
? { done: false, value: from++ } | |
: { done: true } | |
} | |
function iterator() { | |
return this | |
} | |
function nextPromise() { | |
return new Promise(fun) | |
} | |
function fun(resolve, reject) { | |
setTimeout(ready, 100) | |
function ready() { | |
resolve(next()) | |
} | |
} | |
} |
This file contains 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 range(from, to) { | |
return { next: next, [Symbol.iterator]: iterator } | |
function next() { | |
return from <= to | |
? { done: false, value: from++ } | |
: { done: true } | |
} | |
function iterator() { | |
return this | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment