News for hackers. Stuff that matters.
Re:Escape from Callback Hell: Callbacks are the modern goto by epidemian on Friday November 02, 2012 @02:28PM (#4734051)
Yeah, but try to chain three async functions in that style and it becomes evident what pufuwozu meant when they said monads made the code simpler.
Re:Escape from Callback Hell: Callbacks are the modern goto by eurleif on Saturday November 03, 2012 @12:18AM (#4735881)
Deferreds in Twisted make it easy to chain asynchronous code with error handling together:

    def parseData(data):
        return doManyFancyThingsWith(data)
    
    def parsedAsyncResult():
        return asyncResult().addCallback(parseData)

    def callback(data):
        print 'Parsed data:', data
    def errback(err):
        print 'Oh no, something went wrong!'
    parsedAsyncResult().addCallbacks(callback, errback)
If an error is raised by either asyncResult() or parseData(), it will be caught by the errback function, even though parsedAsyncResult() didn't explicitly do anything to pass errors along.
Re:Escape from Callback Hell: Callbacks are the modern goto by pjscott on Saturday November 03, 2012 @12:56AM (#4735940)
That looks like an unnecessarily confusing way of writing this code:

    try:
        data = doManyFancyThingsWith(asyncResult())
        print 'Parsed data:', data
    except:
        print 'Oh no, something went wrong!'
That's the code I would write if I were using Eventlet or Gevent, and it would work just fine. Why settle for less? It's 2012, damn it; we shouldn't have to dick around with deferreds except under exotic circumstances.
Re:Escape from Callback Hell: Callbacks are the modern goto by zenbot on Saturday November 03, 2012 @10:11AM (#4736631)
This is possible in Twisted, using inline callbacks:

  try:
      data = doManyFancyThingsWith(yield asyncResult())
      print 'Parsed data:', data
  except:
      print 'Oh no, something went wrong!'
I find that I use inline callbacks for the common case and work with deferreds directly only when it makes more sense to handle callbacks explicitly.
Re:Escape from Callback Hell: Callbacks are the modern goto by Groxx on Friday November 02, 2012 @06:22PM (#4735055)
I don't know, I find this quite readable:

  foo.doSomethingAsync(function(result) {
    ...
  }, {
  error: function(err) {
    ...
  },
  afterward: function(then) {
    ...
  },
  evenLater: function(wat) {
    ...
  }});
Which is not meant in any way to imply that there's nothing better :) Just that I see no reason to make the comparison to an abnormally-bad version.
Re:Escape from Callback Hell: Callbacks are the modern goto by epidemian on Friday November 02, 2012 @10:20PM (#4735691)
When would the "evenLater" callback be called?

Besides, that's still only one function call. When you need to call many async things one after the other (that's what i meant by "chaining"), that nested callback-based code becomes much much convoluted than the analogous "normal" return-based code.

With promise monads [1] or some sort of language transformation that lets you write code that looks sequential but then is transformed into chained callbacks, you can basically get rid of all those extra callback functions and their associated complexity (i.e. all the mangled "success" and "error" callbacks from chained async functions) and have code that looks normal function calls one after the other. Notice that pufuwozu's example had three consecutive async calls, but it didn't need to create all the callback functions to pass between the calls.

[1]: This is a nice little spec for chainable promises: http://wiki.commonjs.org/wiki/Promises/A. And here's a post that explains how they are used: https://gist.github.com/3889970