Skip to content

Instantly share code, notes, and snippets.

@th507
Last active August 29, 2015 14:23
Show Gist options
  • Save th507/626664612a1ae9893589 to your computer and use it in GitHub Desktop.
Save th507/626664612a1ae9893589 to your computer and use it in GitHub Desktop.
Currying with generator
function *step(fn) {
var arr = Array(fn.length);
for (var i = 0; i < arr.length; i++) {
arr[i] = (yield);
}
var gen = fn.apply(null, arr);
return gen;
}
// Demo function
function timeout(a, b) {
setTimeout(a, b);
return "The end";
}
// Usage:
var res = step(timeout);
var out = res.next();
// arguments for timeout
var args = [console.log.bind(console, 1), 100];
while (!out.done) {
// passing a and b to timeout
out = res.next( args.shift() );
}
console.log(out.value); // -> "The end"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment