Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Created February 7, 2013 05:17
Show Gist options
  • Save puffnfresh/4728748 to your computer and use it in GitHub Desktop.
Save puffnfresh/4728748 to your computer and use it in GitHub Desktop.
jQuery.Deferred's monad
(function() {
function bind(d, f) {
return d.pipe(f)
}
function point(a) {
var d = $.Deferred();
d.resolve(a);
return d.promise();
}
function join(d) {
return bind(d, function(a) {
return a;
});
}
var inner = point("inner"),
nested = point(inner);
bind(nested, function(a) {
alert(a); // [object Object]
});
bind(join(nested), function(a) {
alert(a); // inner
});
})();
@johnbender
Copy link

This makes more sense to me than the other deferreds-as-a-monad examples I've seen, but now I'm curious what the adjunction is and why it's interesting. It looks like it's just the identity functor (which is a monad).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment