Well, frankly, it looks like you're jumping from one sheep to another. Using deferreds does NOT make more sense, and is actually even more difficult to perceive as it's quite counter-intuitive. MVVM is the way to go for web apps. Actually, anything else would be better than this. The methods and approaches jQuery provides should not be used as a mainframe to achieve your goal, they should only be used as helpers here…
Escape from Callback Hell
51–60 of 86 posts
Re: Escape from Callback Hell
#52Earlier quoted context omitted.
That isnt the point, its that you have some nice code that does |var x = someBigFunction()| and at some point in the programs life, something inside someBigFunction turns async, you have a whole restructuring to do.
Then that's a breaking change of the someBigFunction()s interface and it should be treated with care, not just quickly patching on a promise and hope for the best. Javascript is single threaded so when you wrote someBigFunction() you didn't have to think of parallel side effects. If you suddenly pause the execution of someBigFunc() and allow the user to mess around with the interface, the global state that someBigFun…
Re: Escape from Callback Hell
#53https://github.com/Sage/streamlinejs http://en.wikipedia.org/wiki/Continuation-passing_style
Re: Escape from Callback Hell
#54function getItem(id) { return $.get("/api/item/" + id); } $.when(getItem(3), getItem(4)).then(handleSuccess, handleFailure); Maybe I'm taking the point of the example out of context here, but i worry that promises makes it too easy to write non thought through code like this. Why make two separate http-requests for something that should have been possible to do with one? Future prediction: Someone will create code si…
I'm not sure I'm convinced if someone doesn't know how to do a bulk operation they're more likely to look because they've got an extra callback or two.
Re: Escape from Callback Hell
#55I honestly find "callback hell" a lot easier to follow and understand than the vast majority of fixes everyone is coming up with. They're just continuations, seriously, what's everyone's problem? You define a function, it gets access to the current scope, it defines the rest of the program flow. If you feel like your code is nesting too deep, you define the function elsewhere and just reference it by name. Then you d…
> They're just continuations, seriously, what's everyone's problem? That they're badly made continuations, without support from the language. > If you feel like your code is nesting too deep, you define the function elsewhere and just reference it by name. Then you don't get access to the current scope. At the cost of moving stuff out of where it's invoked, so making code harder to read. The problem with callback hel…
Re: Escape from Callback Hell
#56My favorite solution to this problem is a thing the OKCupid developers made called IcedCoffeeScript http://maxtaco.github.com/coffee-script/
Re: Escape from Callback Hell
#57> "This is better, it definitely makes it clearer what exactly is going on" function createItem(item, successCallback, errorCallback) { var req = $.post("/api/item", item); req.success(successCallback); req.error(errorCallback); } How is this more clear than the default jQuery $.post?
Honestly, I didn't like the examples in the article very much. Not only do they fail to show the major advantages promises have, but he also uses named functions everywhere and that is quite silly.
Re: Escape from Callback Hell
#58If you want this problem solved, vote on the generators issue in v8: http://code.google.com/p/v8/issues/detail?id=2355 If v8 implements this, both Firefox and Chrome as well as node.js will have yield, enabling libraries like taskjs [1] to be used. From taskjs.org: spawn(function*() { var data = yield $.ajax(url); $('#result').html(data); var status = $('#status').html('Download complete.'); yield status.fadeIn().pro…
Re: Escape from Callback Hell
#59Re: Escape from Callback Hell
#60> "This is better, it definitely makes it clearer what exactly is going on" function createItem(item, successCallback, errorCallback) { var req = $.post("/api/item", item); req.success(successCallback); req.error(errorCallback); } How is this more clear than the default jQuery $.post?
Really what I meaning to say is that it's much clearer as to what is happening. I was trying to point out that success and error were actually functions on the deferred returned from $.post, not just functions you can pass into $.post (as they once were - hence "circa 2009").