4 ms·
Keeping sanity in callback hell
- SamReidHughes 15y agoIf you think that's callback hell, you try doing it in C++.
- jmah 15y agoThis is a JS (CoffeeScript) implementation of the Maybe monad.
- njs12345 15y agoIt'd be interesting to see a JavaScript-like language with syntactic support for monads (do and friends) - seems like it would enable better abstractions for a lot of this type of thing. There's LambdaScript (https://github.com/valderman/lambdascript https://github.com/valderman/lambdascript) of course, but from the README it doesn't look like it's something you want to use at the moment..
- davidw 15y agoI think Node, Scala, and company will eat Erlang's lunch. However, looking at that code makes you realize that the Erlang actor model with an internal scheduler is pretty nice for a lot of things.
- thesz 15y agoI don't think so. Erlang combines powers of Scala and Node and adds some of its own - selective receive, for example.
- davidw 15y agoYou don't think what? If it's not clear, I am saying that I think Erlang's runtime and API are superior to callback spaghetti. What we may not agree on is whether Node and Scala are going to surpass Erlang in terms of popularity. I think it's only a matter of time. Those two systems are based on languages/runtimes that are orders of magnitude more popular than Erlang and are thus far more accessible. Being far more recent systems, as well, they also avoid some of the strange warts that Erlang must keep around because it's widely deployed enough that it can't be easily changed.
- thesz 15y agoI don't think that Node or Scala alone will eat Erlang's lunch. Combined - maybe, but highly unlikely anyway. By the way, Erlang releases often break backward compatibility. And what are those warts you mentioned?
- moe 15y agoThe syntax.
- davidw 15y agoThe syntax is tough for newbies, strings/lists being conflated is ... not so nice, macros/include files are ugly. Records - ugly! Command line options make it inconvenient for scripting tasks. Lack of libraries (compare with Scala, which has access to pretty much all of Java), no code repositories, ala rubygems. Also: it's more verbose than languages like Ruby, but not as fast, generally, as things like C or Java, putting it at a bit of an odd place for me. They're mostly minor nits, and don't prevent me from liking and using the language, but I'm skeptical about it ever being more than a niche language. Yes, obviously other languages have succeeded without having all those things, that's just the laundry list off the top of my head.
- thesz 15y ago>I'm skeptical about it ever being more than a niche language. Most of languages today are niche. Non-niche languages can be counted with one hand, I guess. If we define "non-nicheness" as a "confidence in the use of language in a new project of majority of programmers/managers" I think we will be left with Java, C# and C++. All other languages will plug holes left from main ones. Erlang, Scala and Node are good niche languages. And I see a Node as a "precursor" for Erlang, actually. It opens lightweight threading for masses. And Node is so clumsy in use (callbacks!) that syntax won't be a big barrier in the use of Erlang. Semantics win over syntax.
- davidw 15y agoIf we look at language popularity, you're right about Java, C# and C++, but you forgot PHP and Javascript. The latter, being the only thing you can code with for browsers, is ubiquitous. Python, Perl and Ruby are all up there as well. There's a fairly sharp dropoff after those. http://www.langpop.com/ http://www.langpop.com/ I'm not sure how Scala will fare, but it's already about as popular as Erlang, and of course Javascript is way past it. Of course, they're nearly all browser users of JS, but still, knowing the syntax and having an idea of how it works makes for a far easier jump than to a functional language like Erlang.
- yesbabyyes 15y agoThe error wrapper is kind of nice, I'll think about adopting it. Basically, though, I find that CoffeeScript's significant whitespace and lack of cruft in general yields pretty nice callback code, with indents as a visual indicator of program flow. Given that you use named functions where appropriate. The same is true for JavaScript as well, but it's easier to accomplish, IMO, in CS. TJ Holowaychuk's JavaScript code, for instance, is very readable.
- moomin 15y agoOr use async.js, or step, which already have addressed this issue. They look nice in coffeescript too.
- Strilanc 15y agoEven better than this is the 'await' functionality coming in C#5. The indentation doesn't increase with each callback, and exceptions are propagated 'across' the callbacks the way you expect.
- unshift 15y agothe fine folks writing the Twisted Python library solved this problem years ago with the Deferred abstraction and the @inlineCallback decorator. there's a little bit of a performance penalty but an enormously huge increase in code readability.