Escape from Callback Hell with Deferreds
Submitted by Yuvi Panda (@yuvipanda) on Sunday, 19 August 2012
- To explain fundamentals of deferreds, using jQuery's implementation for demonstration
- Show how hard-to-understand callback based code can be converted to clearer Deferreds based code
Tired of writing JS with so many nested callbacks that it looks like the Gaza Pyramid turned sideways? Thought you'll never have to write synchronization code again but suddenly find yourself wanting to
wait on 5 callbacks to return before you want to execute something? Hate having to add ugly boilerplate everywhere if you ever want to chain callbacks? Miss the
return statement? Think your code looks ugly because it has way too many levels of indentation?
Start using Deferreds and fix all these! Deferreds are a way of managing callbacks that is painless, elegant and powerful. Using deferreds makes your code cleaner, easier to understand and less error prone. And free ponies too!