by pmontra · 11y ago · 0 comments
.flatMap(_.wrapCallback(one))
.flatMap(_.wrapCallback(two))
...
or .then(Promise.promisifyAll(one))
.then(Promise.promisifyAll(two))
...
are noise making the signal difficult to read. It should come down to nothing more than one | two
plus the error handling clause. That's not JavaScript but you got the idea. Syntax is important. I still have bad dreams about arrays() hell in an app written by a customer with CakePHP 1 (a Rails clone with an approach that didn't suit PHP well). I'll skip over the $this-> mess.I'm not much into asynchronous languages. Is there any examples around with a built in and slim syntax for concatenating callbacks? Thanks.
do one
two
because it's pretty well understood there that syntax can be a big differentiator.What's more is that this syntax adopts for the binding aspect of flatMap. So if two is dependent on one...
do output <- one
two output
which is a shortcut for one >>= two
which might be Promise.promisifyAll(one)
.then(function (output) {
return Promise.promisifyAll(two(output))
})
in JavascriptBut in this specific case of just tying two bit of execution together, I'd observe that Erlang is
One(),
Two()
and Go would be One()
Two()
and that's all regardless of whether One and Two have 0 accesses to IO or do IO dozens of times, and One and Two may also freely call other functions that do arbitrary IO with no further ceremony. The "solution" Erlang and Go offer to callback hell is simply not to have it, and pretty much no matter how you slice it, not having the problem in the first place beats any solution the JS community may or may not settle on.So it kind of comes down to whether you want to shave off ceremony for concurrency or for all monadic/sequencing operations at once. Honestly, that's a tradeoff.
Yeah it's called any language that offers good abstractions for concurrency. Elixir/Erlang, Haskell, Clojure and Go are a few good examples.
function compose(f, g) {
return function inner(x) {
return f(g(x));
}
}
this is not the right kind of composition for something like nested callbacks. For that you need slightly more.Edit:
It's actually the "apply" operator which looks like
function apply(f) {
return function applied(x) {
return f(x);
}
}
Apply and compose work together nicely. For instance, in Haskell apply is ($) and compose is (.), so you can build a compositional pipeline and apply it like f . g . h $ x
or like f $ g $ h $ x
since association goes this way f $ (g $ (h $ x)) one.pipe(two)