O'Reilly logo

Async JavaScript by Trevor Burnham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 3Promises and Deferreds

In 2010, I had this conversation with my prolific colleague Mr. Ajax:

Me:

Hey, would you fetch some data from this URL for me, please?

Mr. Ajax:

I’m on it! Just give me a success callback so I can let you know when I’m done.

Me:

OK, here you go. Thanks.

Mr. Ajax:

Oh, and you should give me an error callback, too. You know, just in case.

Me:

Good point. Anything else?

Mr. Ajax:

Hey, I noticed that there’s some code duplication between those two callbacks! You could move that into a third always callback.

Me:

(impatiently) Alright, I’ll refactor them. Tell you what: why don’t you run now, and I’ll give you the callbacks later?

Mr. Ajax:

(irately) What do I look like, an EventEmitter?

Thankfully, jQuery 1.5 changed ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required