4 ms·
I can get behind the DI dislike, but how are callbacks and promises in nodejs better than async in .NET?
by davidfowl 6y ago
I can get behind the DI dislike, but how are callbacks and promises in nodejs better than async in .NET?
- vosper 6y agoIf you're writing modern nodeJS you're probably not using callbacks or promises (directly) anymore (or using promises a lot less than you used to - Promise.all and Promise.allSettled is about it for me, and that's not that often). The async/await syntax is where it's at. Honestly it's a massive improvement over promises, which I hated reasoning about almost as much as callbacks :)
- charlieflowers 6y agoAnd ... C# _invented_ the async/await syntax. Node, Rust, etc., were all inspired by what C# did.
- dragonwriter 6y ago> If you're writing modern nodeJS you're probably not using callbacks or promises (directly) anymore I get that some people prefer async/await (I actually find explicit Promises clear enough that I don't have any strong preference, myself), but why wouldn't you be using explicit callbacks, which in my JS experience (mostly frontend, but also fairly modern) are super common outside of promises, which is the only place where I see modern JS replacing exlicit callbacks with a different structure.
- vosper 6y agoYeah, callbacks are fine until you get a big stack of them (so-called “callback hell”). Promises were the touted solution to callback hell, async/await the touted solution to endless promises chaining. While async/await is (tasty) syntactic sugar for promises, I think it’s a real improvement over callbacks - it’s one less argument to every function (because of promises) and I think it’s easier to read: there tends to be less nesting.
- dragonwriter 6y agoThe weird thing about async/await is that it gets you out of deeply nested callbacks into, pretty much by definition, exactly equally deeply nested async function calls. The only thing that seems different is that it seems to be more common (but not any more or less supported, since you can obviously have named non-async functions and you can also have anonymous async functions) to call named functions in async/await and anonymous functions defined inline as callbacks with promises.
- Xeronate 6y agoWhat’s not to like about DI? Makes it so much easier to wire up dependent services and refactor those wirings at a later time. If you don’t use DI you have to manually manage everything yourself.
- charlieflowers 6y agoIf you go too far with DI, the saying is "everything happens somewhere else." Some feel it can be needlessly complex to troubleshoot such a system or gain understanding of it if you weren't one of the original authors.
- Xeronate 6y agoDon't need DI to make your program so abstract no one can follow it. Wouldn't even say DI makes it easier to do because you can accomplish the same thing by just newing up objects.
- WorldMaker 6y agoThat's related to my rule of when DI is being abused: if you can't just new up the objects by hand (perhaps in a unit test somewhere), then you should rethink your use of DI. I've seen projects where the dependency object graph exceeded anything you could possibly new up yourself, due to circular dependencies and impossibly complex scope rules. That's definitely a sign of a program so abstract no one can follow it. (Microsoft's very simple DI container that's now standard out-of-the-box in .NET Core has somewhat strict limits on DI scopes and disallows circular dependencies, so it's one of the better ones.)