News for hackers. Stuff that matters.
Re:Fixing the callback spaghetti in node.js by weixiyen on Thursday September 29, 2011 @01:46AM (#3051279)
I wrote a library (30 loc) to handle sequence and parallel flows.

Using only that, I rarely go over 80 character column limit that I impose on myself. There is absolutely zero callback spaghetti whether it's 2 or 25 functions deep in the chain.

Tbh, callback spaghetti only happens to newer async programmers in the same way that a newer programmer will write arrow code with if/else statements.

It's simply not a problem that needs to be addressed other than educating people who are new to node.js with some example tutorials that use an async helper library.

Re:Fixing the callback spaghetti in node.js by wladimir on Thursday September 29, 2011 @02:09AM (#3051335)
So instead of spaghetti code (a tangled mess) it now is macaroni code (small pieces of code everywhere, with unclear execution flow)? :-)

At least that's my experience with structuring asynchronous event-driven programs (without coroutines).

Re:Fixing the callback spaghetti in node.js by n00kie on Thursday September 29, 2011 @02:53AM (#3051433)
Yes :) Still waiting for a proper execSync ( https://github.com/joyent/node/issues/1167 ).

Without it small build/utility scripts turn into macaroni cheese unless you resort back to Python/Ruby/Bash - more languages, harder to maintain.

Re:Fixing the callback spaghetti in node.js by weixiyen on Thursday September 29, 2011 @02:49AM (#3051426)
pretty much but I find it quite readable and maintainable. The execution flow is very clear though if you look at the comments in my gist.
Re:Fixing the callback spaghetti in node.js by tilgovi on Thursday September 29, 2011 @03:18AM (#3051488)
I think this is coroutines under the hood?
Re:Fixing the callback spaghetti in node.js by tlrobinson on Thursday September 29, 2011 @04:18AM (#3051602)
IMHO the fact that the accepted solution to this problem is "use an async helper library" is a bad sign. This is so fundamental it should be part of the language, or at least runtime.
Re:Fixing the callback spaghetti in node.js by weixiyen on Thursday September 29, 2011 @08:06PM (#3054900)
Actually that's a good point. I'd like to change my stance to it would be nice to have this language feature, but there are current workarounds that are decent.
Re:Fixing the callback spaghetti in node.js by franze on Thursday September 29, 2011 @02:39AM (#3051406)
i agree with you, i don't see it as a big issue. anyway: i'm excited that there are new/other paradigms out there. i'm not yet convinced that this the ultimate (if there is such a thing) solution, but i will give it a try.
Re:Fixing the callback spaghetti in node.js by gfxmonk on Thursday September 29, 2011 @01:51AM (#3051294)
got a link? I'd be interested to know how it deals with errors / exceptions, for example.
Re:Fixing the callback spaghetti in node.js by weixiyen on Thursday September 29, 2011 @02:00AM (#3051311)
Re:Fixing the callback spaghetti in node.js by snprbob86 on Thursday September 29, 2011 @04:07AM (#3051584)
I've been using: https://github.com/caolan/async

It looks pretty decent in CoffeeScript:

  blah = (done) ->
    async.series [
      (next) -> foo next
      (next) => @bar.baz 1, 2, 3, next
      (next) ->
        x = y
        z w next
    ], done
Re:Fixing the callback spaghetti in node.js by gfxmonk on Thursday September 29, 2011 @03:18AM (#3051485)
Looks concise, but doesn't much help with the fundamental problems of callback code - exceptions don't work right, and "callbacks all the way down" whenever something starts having any async computation.
Re:Fixing the callback spaghetti in node.js by weixiyen on Thursday September 29, 2011 @04:19AM (#3051603)
what do you mean by this -> "callbacks all the way down" whenever something starts having any async computation.
Re:Fixing the callback spaghetti in node.js by gfxmonk on Thursday September 29, 2011 @08:56AM (#3052241)
Imagine function a calls b() and function b calls c(), and they're all sync.

Now, let's say c() changes and needs to call someAsyncFunction() and provide a callback. Which means c itself needs to take a callback. Which means b needs to provide a callback, so b needs to also take a callback, so a needs to provide a callback. And so forth.

Callbacks are infectious - once anybody in the call stack needs one, everybody needs one even if they just pass it on down the stack. Unless you don't need to do anything with return values, but that's fairly rare.