4 ms·
I think what this really gets at is that callbacks are a shitty concurrency primitive. They certainly make sense from an implementation perspective (I've got t
by ohyes 10y ago
I think what this really gets at is that callbacks are a shitty concurrency primitive.
They certainly make sense from an implementation perspective (I've got this interpreter and I want to do concurrency... I know, I'll just use it to trampoline callbacks).
But callbacks are too low level to reason about easily to some extent, it's kind of like writing everything as a series of goto statements. Sure you can do it, but good luck following it.
The advantages of asynchronous execution are lost. This isn't limited to nodejs, I've experienced similar problems with Python twisted...
Channels (such as in clojure/core.async) or lightweight processes and messaging (such as in erlang/beam), are much more enjoyable to work with.
This shouldn't really be a shock, however, they're higher level abstractions based on real computer science research by people who really thought about the problem.
- azernik 10y agoOn the subject of nice higher-level concurrency abstractions, I'm very taken with the current fad for Promises/Futures (a la ES6 or Scala), which are basically a translation of the Haskell IO monad to plain English.
- gclaramunt 10y agoIsn't closer to the Async monad?
- int_19h 10y agoCallbacks are ugly when you have to deal with them directly. But a little bit of syntactic sugar to capture the current continuation as a callback - such as C# await, or the now-repurposed yield in Python - makes it very easy and straightforward to write callback-based async code.
- davidjnelson 10y agoBabel and regenerator let you use await in es/js too.
- flamedoge 10y agoPython does have await from asyncio
- int_19h 10y agoApparently, I wasn't paying as much attention to the state of async in Python 3.5 as I should have. Looks like they actually have the most featureful implementation of that right now, complete with async for all constructs where it can be sensibly implemented and provide some benefits (like for-loops and with-blocks).
- jackweirdy 10y agoFortunately JavaScript is a flexible enough language that you don't only have to use the primitives it provides. Promises (which were pure JS before they were ever added to the language) are a more-composable structure for reasoning about concurrency. I don't think I've put more than one line of callback-style code into production per month.
- Klathmon 10y agoAnd async/await are such a game changer its amazing. I finally bit the bullet and tried it even though it's still not final syntax, it simplified most of my code while keeping the execution exactly the same. It really is a night and day difference.