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 Sunday, 19 August 2012

Section: Zen of JavaScript Technical level: Intermediate Session type: Tutorial

Abstract

  • 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

Outline

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!

Requirements

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.

Links

Comments

  • Yuvi Panda (@yuvipanda) Proposer 6 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) 6 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.

      Cheers

      • jaseem abid (@jaseemabid2) 6 years ago

        Don’t miss promises.

      • Yuvi Panda (@yuvipanda) Proposer 6 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) 6 years ago

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

  • Aditya (@netroy) 6 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 with Twitter or Google to leave a comment