Live data from Hacker News

Callback Hell (2016)

callbackhell.com

11–20 of 170 posts

Re: Callback Hell (2016)

#11

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.

That's how it's done in Go. You write plain, "blocking" code, and if it blocks, the Go runtime will just schedule another goroutine (green thread). In Go there's no need for callbacks or littering your code with `await` keywords to write concurrent software.

Re: Callback Hell (2016)

#13
Lot of blabbling for a simple concept: do not abuse nesting. It's the case with control statements, it's still the case with functions, and even more with async functions.

Oh, and callbacks definitely exist in other languages, like C.

What's the deal with this trend of setting up a whole website for a (basic) blog post?

Re: Callback Hell (2016)

#14
post #5
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, more recently, Observables. Simply put: A promise resolves to a single value asynchronously, an observable resolves to (or emits) multiple values asynchronously (over time). Taken from here: http://stackoverflow.com/questions/36064303/what-are-the-dif...

Oh I also forgot async/await

"The purpose of async/await functions are to simplify the behavior of using promises synchronously and to perform some behavior on a group of Promises. Just like Promises are similar to structured callbacks, async/await is similar to combining generators and promises." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re: Callback Hell (2016)

#15
post #4
post #3

Last updated in 2012. A lot has changed since.

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.

Re: Callback Hell (2016)

#16

Lot of blabbling for a simple concept: do not abuse nesting. It's the case with control statements, it's still the case with functions, and even more with async functions. Oh, and callbacks definitely exist in other languages, like C. What's the deal with this trend of setting up a whole website for a (basic) blog post?

SEO.

Re: Callback Hell (2016)

#18
post #4
post #3

Last updated in 2012. A lot has changed since.

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(err, data) {
                    if (err) {
                        return callback(err)
                    }
                    callback(undefined, processData(data))
                })
            }catch(e) {
                callback(e)
            }
        })
    }
After:

    async function getPhoto() {
        const data = await fetch("/favorite_photo_id")
        const dataJson = await data.json()
        const photoData = await fetch(dataJson.url)
        return processData(photoData)
    }
The example may be a bit contrived, but you get the idea.

Re: Callback Hell (2016)

#19
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.

He addressed more modern solutions at the bottom. I think, he is trying to evangelize "you should write readable code, no matter of the limitations of the language."

Re: Callback Hell (2016)

#20
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.

The page does mention those.
Post reply on HN