
Faking Co-Routines, or Why Callback Hell Is Over (2014) - dyoder
https://www.pandastrike.com/posts/20141114-faking-co-routines
======
erikpukinskis
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.

~~~
jestar_jokin
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.

~~~
fao_
If you're repeating yourself, then you could possibly abuse higher class
functions to automatically generate error handling?

~~~
doublerebel
Yes, there are libs like errTo [1] and iced-error/make_esc [2]. Not to mention
Promise.promisify(). Long solved problem.

[1]:
[https://www.npmjs.com/package/errto](https://www.npmjs.com/package/errto)

[2]: [https://www.npmjs.com/package/iced-
error](https://www.npmjs.com/package/iced-error)

------
outside1234
Please: Do not use CoffeeScript in your examples.

It is not common to have "readability" in CoffeeScript and it restricts a
significant number of readers from being able to understand the code sample.

~~~
mchahn
> restricts a significant number of readers from being able to understand

Can you seriously say you can't read it? It seemed easy to me.

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

~~~
ludamad
There really shouldn't be an difficulty seeing how it maps to JavaScript.
CoffeeScript is pretty clean for code examples.

~~~
Azkar
It's not that it's difficult to understand, it's about comparing apples to
apples.

------
pspeter3
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?

~~~
jestar_jokin
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.

------
scriby
[https://github.com/scriby/asyncblock-
generators](https://github.com/scriby/asyncblock-generators)

That's a control flow solution I wrote on top of generators to make it a
little easier to manage parallel tasks, timeouts, error handling, and so on.

I originally made asyncblock, which was based on fibers a few years ago. This
module uses the same underpinnings as asyncblock, just based in generators
instead of fibers.

------
amelius
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).

------
teddyh
What’s with the HUGE FONT SIZE?

------
raspasov
Use ClojureScript + core.async and get some work done.

~~~
hellofunk
Absolutely, cljs is a top-shelf way to work in JS. And core.async is
fantastic.

