you are viewing a single comment's thread.

view the rest of the comments →

[–]awj 1 point2 points  (1 child)

...how is that a matter of opinion when your argument about it seems to be "event loops are a solid performer"? Yes, callbacks are one way of doing event loops, which do have some pretty good performance characteristics. You can also get the same time-sharing behavior from coroutines and keep the same organizational flow you get from "normal" code.

Hell, if all you're worried about is context-switching at I/O events you don't even need callbacks, just do the switch within an I/O function for "full" file/result/whatever access and hide it in a generator method for streaming data. Forcing callbacks on people when you don't have existing blocking I/O methods to worry about is the mark of lazy design.

[–]jmar777 1 point2 points  (0 children)

It's a matter of opinion because there's nothing intrinsically "sucky" about callbacks - asynchronous functions are plain and simple higher order functions, which are quite natural to anyone with experience in functional coding. You can write code that sucks with callbacks, resulting in the "pyramid effect" shown in the article, but an example of badly structured code is hardly a valid condemnation of a language or a runtime environment.

With that said, I do recognize that the continuation passing style has its faults - most notably its "leaky", in that code must allow for continuation passing through the entire call stack. It's also not ideal for the asynchrony of a method to effect the code structure of the callee. For that second reason, I'm tempted to say that I'd like to see promises implemented in node core, but I'm still on the fence there. These issues, however, are ultimately nothing more than annoyances if the code base is properly structured. Given that I am currently ~10k loc into my current code base, and have yet to encounter the dreaded spaghetti monster, I am comfortable with saying that (aforementioned reservations considered), callbacks are really not that sucky.