3 ms·
So instead of encouraging you to actually fix the bad nested callback code, you can dress it over with fancy syntax. Well done on making a whole language thoug
by TheZenPsycho 13y ago
So instead of encouraging you to actually fix the bad nested callback code, you can dress it over with fancy syntax.
Well done on making a whole language though. It's pretty challenging and I hope you do well.
Aside: This seems very similar to narrativeJS in concept
http://www.neilmix.com/narrativejs/doc/ http://www.neilmix.com/narrativejs/doc/
- rzimmerman 13y agoThanks. I think the main issue with JS callback syntax is that it quickly becomes unmaintainable. Hopefully this fixes a lot of that by abstracting away the mechanics. I don't think there's something fundamentally wrong with the "relinquish control and wait for I/O" model, so I tried to make that explicit here.
- deleted 13y ago[deleted]
- mbrock 13y agoWhat do you mean by "actually fix?"
- TheZenPsycho 13y agoStop nesting anonymous functions. Un-nest them, name them, and encapsulate the process in a monad.
- TheZenPsycho 13y agomonad* *jQuery or promises like API object, whereupon you place methods- some of which may be asynchronous. an asynchronous operation returns an object with a method that performs the next action and optionally takes a callback, passing in the results of the previous asynchronous operation as a value- thus flattening the callbacks out into a sequence instead of a nesting. Constructed properly, you can create monad combinators/transformers, to do things like automatically wrap every step in your chain with exception handling. Using common JS promises libraries gives you error handling for free.
- nbouscal 13y agoYou do know that jQuery isn't a monad, right? That's the second time in this thread that you've called it one. Simplest demonstration that it isn't: try defining join.
- TheZenPsycho 13y agojquery-like. I never claimed that that jquery is a monad. I know it isn't. But also, I don't want to be one of those guys that throws around the word monad expecting everyone to understand what I'm talking about. as for defining "join", help me understand. How is jquery "add" not essentially that? And why pick the fmap,join formulation instead of the bind,return formulation of a monad?
- nbouscal 13y agojQuery's add function has a few different formulations depending on what argument you pass, but it's basically either M a -> a -> M a (if you pass it an html string, an element, or a selector) or M a -> M a -> M a (if you pass it another jQuery object) where here M refers to the jQuery wrapper and the first argument refers to `this` (obviously these type signatures are an approximation since JavaScript doesn't really work this way). join is M (M a) -> M a, which is very different. The reason I picked join is because (in my view, anyway) it's the easiest demonstration of how jQuery is not a monad. There's no way to even get something of type M (M a) in jQuery, because the jQuery lifting operation is idempotent. The same exact problem exists with return, but I've found that people have a hard time seeing it with that example. The problem I have with you referring to jQuery as a monad, even by analogy, is that jQuery really really isn't a monad. It's only very loosely analogous, and that analogy is more likely to teach people the wrong things about monads rather than the right ones.
- TheZenPsycho 13y agoWell that is a fair enough concern, and I notice I used the word "Monadic" to describe jquery carelessly, to mean "Monad-like" though that isn't really what "Monadic" means. For the case in hand though, you can get to almost but not quite really technically a monad, and still cure most of the nested callback headaches. One way I think about monads is they are a strategy for turning nested functions into sequentially composed functions. Which is, pretty much what we want, right?