3 ms·
shenanigans. Pyramids (at least in js) can be easily avoided by simply naming your functions. Treating them like the first class objects they are. Naming the fu
by boopbeep 13y ago
shenanigans. Pyramids (at least in js) can be easily avoided by simply naming your functions. Treating them like the first class objects they are. Naming the function means you are no longer going to an arbitrary code block, but instead going to a concept whose name, doc string, and (through hoisting) position on the page illuminate its purpose.
Callbacks aren't bad. Pyramids are bad. Stop writing pyramids.
Nodejs also establishes a nice api for callback functions-- in particular, callbacks are defined with an `error` and a `data` argument. You handle the error if it is non-null, otherwise execute `data`.
If you want to avoid callbacks, Node also provides event emitters, and streams. Streams in particular provide a nice api for dealing with event based programming.
- stefanve 13y agoI'm fairly new to node.js, but having done some decent work in AS3/Flex (admittedly years ago) I'm a bit amassed that they didn't take a look how Adobe took on some of these "problems". I think it is a pity that the don't have callbacks for onError and onResult for instance. It would be easy to do and code would look nicer and with less noise. I suspect there are other niceties that could be learned from AS3/Flex and async/ event loop programming.
- fauigerzigerk 13y agoI disagree. Callback based programming forces you to modularize around asychronous/IO calls instead of semantic cohesion. Naming functions doesn't solve that problem. On the contrary, you get seperate lexical units that are not separately reusable and make no sense on their own. They are simply fragments of code that's supposed to be executed before or after some IO operation inside another function.