Ask HN: Is callback hell exaggerated?
11 points| tonyle | 9 years ago
Most information on it seems to start off as a T.V. infomercial for some library. Don't suffer from callback hell, use X!
I can barely find anything on the internet about callback hell before 2010 to indicated that it was a real problem at all with javascript.
nostrademons|9 years ago
Even then, I think it's a bit overstated - people were writing event-driven servers in C++ before Javascript was even invented. I do think that being able to use semicolons to sequence two statements is a lot more concise than nesting a chain of closures, though.
angersock|9 years ago
Let's use NodeJS with Express and with pg-node. Let's say you have a database.
You now have a pyramid something like 5 levels deep. Callback hell is in fact a thing.However, I've found that pipeline of promises almost completely solve that problem.
tonyle|9 years ago
I mean if I didn't have the option of using promises, I would have probably written something like this.
function handler(callback){
gravypod|9 years ago
MalcolmDiggs|9 years ago
It's not black and white, and no library is going to solve all the issues. A pragmatic JS engineer is going to have to use many different tools in their toolbelt on a routine basis, it's just a matter of choosing the right tool for the job (which includes taking into account the environment you're working in and the code that already exists).
But in general, in my experience, callback hell is often a sign of an upstream design-pattern issue. If the procedure you're writing needs to call a series of 20 functions, in sequence, (and wait for them all to callback one after the other) you can probably implement it in a clean way that doesn't require your code to move evermore to the right. Frameworks like Express (which passes around "next"), and Mocha (which passes around "done") set great precedent of alternative ways to conceptualize those kind of issues. Achieving this in practice often means writing a combination of promises, callbacks, and other things.
unknown|9 years ago
[deleted]
zzzcpan|9 years ago
smt88|9 years ago
It depends on how much of your business logic must be synchronous and how much it relies on asynchronous operations. It also depends on what you consider to be hellish.
async/await is still cleaner than even a single promise, so you might as well use it.
nojvek|9 years ago
Promises certainly help but you also see abuse of then callback hell where rather than chaining, someone would nest the thens.async await is also great for debugging.
antoineMoPa|9 years ago
Here is my nodejs algorithm so far:
kazinator|9 years ago
unknown|9 years ago
[deleted]
znpy|9 years ago
Kinnard|9 years ago
antoineMoPa|9 years ago