JSFoo 2012

It’s like kung fu with JavaScript and objects! All about building full size apps in JavaScript.

Escape from Callback Hell with Deferreds

Submitted by Yuvi Panda (@yuvipanda) on Aug 19, 2012

Section: Zen of JavaScript Technical level: Intermediate Session type: Tutorial Status: Confirmed & Scheduled


  • 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!


Intermediate level of Javascript knowledge would be sufficient.

Speaker bio

Yuvi Panda is a 21 year old open source geek who chases shiny new things way too often. He wrote major parts of the Wikipedia Android/iOS app (JS/Phonegap), and the upcoming Wiki Loves Monument app (also JS/PhoneGap). He was yearning for a threaded model in Javascript until he found Deferreds. Amongst his hobbies is screaming 'USE DEFERREDS!' at anyone claiming to use Javascript. He's currently a student at KCG College of Technology.



  • Yuvi Panda (@yuvipanda) Proposer 7 years ago

    Deferreds basics, waiting on multiple deferreds, + transforming a few hairy callback hell snippets into deferred based ones wouldn’t be enough?

    • Aditya (@netroy) 7 years ago

      cool. :)

      BTW, i didn’t say it won’t be enough. I just said it’d be a better talk if you do mention other control flow methods, so that the noobs don’t walk out thinking the deferreds are holy grail for writing async code. I personally like callbacks sometimes, while caolan’s async is awesome in certain cases.

      Deferreds/Promises make great chainable APIs, but what if you had to do an asynchronous map, reduce or filter on an array of 5000 items ? creating 5000 deferred objects would work, but won’t be a good idea.

      I know I’m being a bit anal, but the talk is marked as Intermediate.


      • jaseem abid 7 years ago

        Don’t miss promises.

      • Yuvi Panda (@yuvipanda) Proposer 7 years ago

        Ah, right. In that case, perhaps I should mark it as beginner. I’m unsure of the beginner-intermediate-advanced levels - it is possible that spending too much time telling people ‘NO, it is not inti but int i‘ has skewed my perspective.

    • Sunil Pai (@threepointone) 7 years ago

      I think it’s just fine for half an hour, actually.

  • Aditya (@netroy) 7 years ago

    Cool.. but you might consider covering other control-flow methods for dealing with async code and comparing them to deferreds.
    Deferreds alone for 30 minutes wouldn’t make a good talk.
    Trivia: Deferreds were introduced by Dojo back in 2006

Login to leave a comment