Skip to content

Instantly share code, notes, and snippets.

@Williammer
Last active November 21, 2016 03:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Williammer/d952cd5be07896158d38 to your computer and use it in GitHub Desktop.
Save Williammer/d952cd5be07896158d38 to your computer and use it in GitHub Desktop.
Thunk pattern - which seems to be one example of Partial application, is subroutine that helps.
function thunkify(fn) {
return function() {
var args = [].slice.call( arguments ),
ctx = this;
return function(cb) {
var called;
args.push(function(){
if (called) return;
called = true;
cb.apply(null, arguments);
});
try {
fn.apply(ctx, args);
} catch (err) {
cb(err);
}
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment