HNHacker News
TopNewBestAskShowJobs

boopbeep

0 karma · joined August 16, 2013

submissionscomments
boopbeep··on Callbacks as our Generation's Goto Statement
this use case is inherently more complicated than any of the control flow structures you mention here. You are introducing a new closure, and you don't know when the function is going to be executed.
boopbeep··on Callbacks as our Generation's Goto Statement
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.