Skip to content

Instantly share code, notes, and snippets.

@kmaglione
Created December 20, 2011 21:01
Show Gist options
  • Save kmaglione/1503258 to your computer and use it in GitHub Desktop.
Save kmaglione/1503258 to your computer and use it in GitHub Desktop.
/**
* 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