Live data from Hacker News

Faking Co-Routines, or Why Callback Hell Is Over (2014)

pandastrike.com

1–10 of 24 posts

Re: Faking Co-Routines, or Why Callback Hell Is Over (2014)

#4
I have yet to see an "improvement" on callbacks, whether it's promises or fibers or generators, where the benefit in readability is worth the havoc it wreaks on my ability to debug the program.

These days I write JavaScript using only functions, literals, variables, and the occasional prototype and it's amazing.

I think many programmers have a desire to believe they are working on complex problems that demand sophisticated tools. I remember learning Ruby and being excited whenever I found a reason to write a DSL. In retrospect it was unnecessary every time. In every case the code would've been clearer if I had just stuck with functions and kept refactoring until I had the right interfaces and data structures.

It helps to remember

    function a() {
      b(function() {
        //etc
      })
    }
is equivalent to

    function a() {
      b(c)
    }

    function c() {
      //etc
    }
which is not particularly more verbose. And as a side benefit, refactoring that way gives you an opportunity to make c() self-documenting.

Re: Faking Co-Routines, or Why Callback Hell Is Over (2014)

#6

I have yet to see an "improvement" on callbacks, whether it's promises or fibers or generators, where the benefit in readability is worth the havoc it wreaks on my ability to debug the program. These days I write JavaScript using only functions, literals, variables, and the occasional prototype and it's amazing. I think many programmers have a desire to believe they are working on complex problems that demand sophist…

> It helps to remember

> function a() { b(function() { //etc }) } is equivalent to

> function a() { b(c) } function c() { //etc } which is not particularly more verbose. And as a side benefit, refactoring that way gives you an opportunity to make c() self-documenting.

It's not always equivalent, since you can have closures.

Re: Faking Co-Routines, or Why Callback Hell Is Over (2014)

#7
post #3

I still don't get why async/await is inherently better than using Promises/Streams. Is it purely a syntax difference or is there a semantic difference as well?

I believe async/await uses promises behind the scenes. It just means invoking code looks nicer, i.e writing "var res1 = await prom1(a); await prom2(a, res1)" is marginally cleaner than "return prom1(a).then((res1) => prom2(a, res1));", in the sense that you're just writing statements rather than chaining method invocations.

Re: Faking Co-Routines, or Why Callback Hell Is Over (2014)

#8
The problem with javascript coroutines is that you can only yield from within the generator itself, not from a called function.

This makes it impossible, for instance, to write a nice I/O library that is to be called from within a generator (the library is supposed to yield on a blocking situation).

Re: Faking Co-Routines, or Why Callback Hell Is Over (2014)

#9

I have yet to see an "improvement" on callbacks, whether it's promises or fibers or generators, where the benefit in readability is worth the havoc it wreaks on my ability to debug the program. These days I write JavaScript using only functions, literals, variables, and the occasional prototype and it's amazing. I think many programmers have a desire to believe they are working on complex problems that demand sophist…

Unfortunately, Node.js decided that every callback should accept an error as the first argument to every callback. If you're chaining a bunch of callbacks, it's tedious and error-prone to add boilerplate to check for an error and handle it consistently in every callback, and violating the DRY principle. It's not easy to simply propagate the error to a higher-level handler.

Re: Faking Co-Routines, or Why Callback Hell Is Over (2014)

#10
post #5

I don't understand why he talks about a javascript problem being solved, then goes on to show us examples in coffee script.

There really shouldn't be an difficulty seeing how it maps to JavaScript. CoffeeScript is pretty clean for code examples.
Post reply on HN