Last active
November 21, 2016 03:32
-
-
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.
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
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