Live data from Hacker News

Callback Hell (2016)

callbackhell.com

21–30 of 170 posts

Re: Callback Hell (2016)

#21
post #15
post #4

Earlier quoted context omitted.

Could you elaborate? Coming from Lua I always found the nesting of anonymous functions messy and wondered why they weren't done as in the article.

Promises and async/await. This page is so outdated it shouldn't exist.

TBH it shouldn't be on the front page of HN.

Re: Callback Hell (2016)

#22

As a curious and perhaps naive aside, I've been wondering why the programmer should need to care about asynchronous execution of code at all. Can't it all be abstracted under a procedural layer and let the OS worry about not blocking anything? The advent of promises, async.js, and other paradigms tell me that people still kind of want to write code that does one thing after another, then another, then another.

They are called threads. Have fun with that :)

Re: Callback Hell (2016)

#23
post #3

Last updated in 2012. A lot has changed since.

This is mentioned in the last section of the article: "What about promises/generators/ES6 etc?"

There was a lot less support for them in the ecosystem back then, so you had to make certain trade-offs that you no longer need to make today.

Re: Callback Hell (2016)

#26
post #4

Earlier quoted context omitted.

Could you elaborate? Coming from Lua I always found the nesting of anonymous functions messy and wondered why they weren't done as in the article.

You can now use async/await directly in node (no transpiler necessary), and both Babel and TypeScript can compile them for browser use. Also there are now many more libraries that use native ES6 promises. Before: function getPhoto(callback) { someCustomHttpGet("/favorite_photo_id", function(err, data) { if (err) { return callback(err) } try { var dataJson = JSON.parse(data) someCustomHttpGet(dataJson.url, function(er…

Your try catch is useless here, AFAIK JSON.stringify doesn't throw. and you can't catch someCustomHttpGet() "exceptions" if the latter is supposed to be asynchronous.

relevant:

http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...

Re: Callback Hell (2016)

#27
post #26

Earlier quoted context omitted.

You can now use async/await directly in node (no transpiler necessary), and both Babel and TypeScript can compile them for browser use. Also there are now many more libraries that use native ES6 promises. Before: function getPhoto(callback) { someCustomHttpGet("/favorite_photo_id", function(err, data) { if (err) { return callback(err) } try { var dataJson = JSON.parse(data) someCustomHttpGet(dataJson.url, function(er…

Your try catch is useless here, AFAIK JSON.stringify doesn't throw. and you can't catch someCustomHttpGet() "exceptions" if the latter is supposed to be asynchronous. relevant: http://journal.stuffwithstuff.com/2015/02/01/what-color-is-y...

Yeah, I messed it up when quickly typing the code without thinking, it should've been JSON.parse, I fixed it now.

Also regarding that article: you can call async functions from non-async functions - you will simply get the Promise object instead of the data.

If you're using TypeScript, your editor will immediately highlight the error if you try to use that Promise object as though it were the data, so zero chance of getting it wrong. Try it yourself - https://goo.gl/vDk3Gz - hover over the `n` inside the `formatName` call.

Re: Callback Hell (2016)

#28
post #7

Not sure this problem goes away by giving names to anonymous functions, which seems to be his suggestion

Nop, but it can be a bit better by using predefined functions rather than inline ones.

Probably better, but the callback hell stems from a broken up control flow; where we cannot read / understand what's going on with a linear (top to bottom) scan of the source code (even for small sections of code).

I think programming where we have state and things can happen at any time, where programs wait for events or something similar has proven to be difficult to write/understand/maintain. The 30+ years it has taken to write the (unfinished) Herd micro-kernel is a good example.

Re: Callback Hell (2016)

#29
As a C programmer working with a large codebase, I have come to HATE callbacks.

Seriously, the worst feeling ever is tracing through a huge function tree, only to run into function pointer dereference. Then you have to go on a wild goose chase to find out when, where and what it will be assigned to.

STATIC TYPES PEOPLE.

Post reply on HN