Created
January 11, 2014 07:58
-
-
Save aaronpowell/8368286 to your computer and use it in GitHub Desktop.
A script for running generator functions
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
let runner = function (fn) { | |
if (typeof fn !== 'function') { | |
throw 'A generator function is expected'; | |
} | |
var done = false; | |
var gen = fn(); | |
var val; | |
while (!done) { | |
let obj = gen.next(val); | |
done = obj.done; | |
val = obj.value; | |
} | |
}; |
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
var doStuff = function* () { | |
console.log(+new Date); | |
let x = yield go(); | |
console.log('yeaaaaaaah', x); | |
let y = yield go(); | |
console.log('yeaaaaaaah', y); | |
console.log(+new Date); | |
}; | |
var go = function () { | |
for (let i = 0; i < 10000000; i++) { | |
// | |
} | |
return 'foo'; | |
}; | |
runner(doStuff); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment