Javascript Thunk
(function() {
function thunkify(fn) {
return function() {
var args = [].slice.call(arguments);
return function(cb) {
args.push(cb);
return fn.apply(null, args);
};
};
}
function foo(x, y, cb) {
setTimeout(function() {
cb(x + y);
}, 1000);
}
var fooThunkory = thunkify(foo);
var fooThunk1 = fooThunkory(3, 4);
var fooThunk2 = fooThunkory(12, 12);
fooThunk1(function(sum) {
document.write('fooThunk1: ' + sum + '<br>')
});
fooThunk2(function(sum) {
document.write('fooThunk2: ' + sum)
});
})();