Callback Hell
If you’ve ever written any Javascript that contacts or lives on the server, you’ve probably experienced Callback Hell by now. It is a horrible affliction that Javascript can suffer from as a result of many nested callback functions that are triggered once a request has been completed. Often times, this results in a whole bunch of anonymous functions that make your Javascript nested 2^84 levels deep which causes a lot of messiness and unreadability. There are a few simple ways you can avoid callback hell; you can read about them more in-depth with code examples by following the link but here is a good gist:
Name your functions! –Having named functions makes your code much easier to read and keep track of, especially when debugging or making changes!</span>
Keep your code shallow! – If your Javascript is indented more than about 3 levels, consider breaking it up further into smaller functions. This makes much more manageable chunks that are easier to test and maintain.
Modularize! – If you are using nodejs or requirejs you should be keeping related functionality in separate module files then require
-ing them as they are needed. This makes the code easier to read and keep track of for other developers, and avoids polluting the global namespace.
Check out the examples over at callback hell for more information, and hopefully you will be able to avoid the fiery depths!
Edit: In reply to Yussuf’s comment, you should also check out jQuery Promises for asynchronous AJAX requests in JavaScript. I have been making use of them extensively via Backbone.js and they are really useful. Here are a couple of handy articles on the subject: