Created
December 20, 2011 21:01
-
-
Save kmaglione/1503258 to your computer and use it in GitHub Desktop.
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
/** | |
* Makes the passed function yieldable. Each time the function calls | |
* yield, execution is suspended for the yielded number of | |
* milliseconds. | |
* | |
* Example: | |
* let func = yieldable(function () { | |
* util.dump(Date.now()); // 0 | |
* yield 1500; | |
* util.dump(Date.now()); // 1500 | |
* }); | |
* func(); | |
* | |
* @param {function} func The function to mangle. | |
* @returns {function} A new function which may not execute | |
* synchronously. | |
*/ | |
yieldable: function yieldable(func) | |
function magic() { | |
let gen = func.apply(this, arguments); | |
(function next() { | |
try { | |
util.timeout(next, gen.next()); | |
} | |
catch (e if e instanceof StopIteration) {}; | |
})(); | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment