4 ms·
The 'asynchonous problem' in JS is twofold. It makes it more difficult to reason about the program, and is hell on readability, and readbility matters. Support
by dpweb 10y ago
The 'asynchonous problem' in JS is twofold. It makes it more difficult to reason about the program, and is hell on readability, and readbility matters. Support, maintenance, etc. The lack of an ability to structure async ops in a simple way is the biggest drawback in the language.
Callbacks of course have the drawbacks mentioned. Sure, you can separate the functions, smaller functions and that too is preferred, but still you get code that can be difficult to reason about and read.
Promises, for all the hype, in my view clearly did not help matters much. Promise code can be as weird looking or worse than callback code. Promises did not solve the problem.
Async/await IS the solution. I've started converting callback and promise code to it. It's a great new enhancement.
- woah 10y agoI used to think that promises were pretty bad, but then i started using async/await. Now I think of promises as async/await with a lot of extra cruft. I have a colleague who prefers promises because they force programmers to break logic into small functions.
- foreigner 10y agoI can't wait for async/await to be added to node is proper!
- minitech 10y agoPromises did solve the problem; async/await is just syntactic sugar for them. The important thing is being able to start a task and pass around its value on completion without worrying whether that completion has happened yet. If you didn’t see an advantage to promises, you might have just been using them like callbacks.
- ufo 10y agoPromises are just as bad as callbacks in most of the code organization issues and I agree with you that they don't actually solve "callback hell". However, one thing that promises (and other library-based solutions) do better than hand-written callbacks is exception handling. By default, promises will capture and propagate and exceptions thrown inside them, which is something that is very easy to forget to do with hand-written callbacks.
- erikpukinskis 10y agoI can't disagree with you more strongly. Async/await and promises both take control flow complexity, of which callback hell is a symptom, and sweep them under the rug. Whereas before you had a clear (if convoluted and in need of refactoring) control flow, now with async/promises you shove it all into an invisible state machine, with transient data structures that are either hard to inspect (promises) or totally inscrutable (async/await). No thanks. I like callbacks. Here's my test: can you reason about what a function might do with control flow just by looking at its call site, not its source code? With callbacks you can. With async/await you can't.