Skip to main content

Promises and Deferreds - How I Stared To Like Them



Lately I've started to believe that promises are really useful concept. I used to think of them as some mumbo-jumbo invented by folks who can't get the idea that a function can be stored in a variable and the problem they are trying to solve is easy enough to deal with without any libraries.

My first step to understand them was to read: You're Missing the Point of Promises

After that I started to see something in them but couldn't find any real need for them. At that time I joined a new company and I was lucky enough to participate in the first HackDay where we started to implement mashup site that needed to get data from a bunch of urls and then start UI.
It was a perfect use case for promises, when all data is downloaded ("when") we can call UI initialization. As we used jQuery to speed up hacking I used jQuery promises with ajax calls. We found really good post by Edwin Martin Deferred and promise in jQuery and I finally got the idea behind promises and deferred in practice and how to use them in jQuery.

A couple of weeks later I read Async JavaScript by Trevor Burnham. What I found there was another good example of promises (I really recommend this book! :) This was like a cherry on the pie.


Summary:
Promises are a really elegant solution to complicated asynchronous code. You don't have to use them with all your ajax calls, or routing in Node.js but there are situations when it's really hard to manage your code without them.

Promises aren't really about removing asynchronous calls and callbacks, they just make it really easy to have a list of callbacks or one callback for many functions.

There is also a new specification called Promises/A+ that somewhat simplifies idea and API. If you're interested in promises you can find differences between Promises/A+ and CommonJS Promises/A on the official page: http://promises-aplus.github.com/promises-spec/differences-from-promises-a

I'm planning to show more about promises with code in future posts.

Image is from: Promises/A+.

Comments

Popular posts from this blog

Course Review: The Bitfountain iOS 8 with Swift Immersive

This is second big iOS learning project. For first one read iOS Programming book review. As I only have two of them done I have to compare them somehow.

I am mindful that it took me a really long time to finish it (since last autumn). For a couple of months this year I didn't even have the internet to learn it, so it could not be helped. But last half of the June I've spent watching the videos every day and managed to finish it before any new internet problems.

So how was it?
In my opinion, it's great if you're starting out. There are a lot of basics, and seeing how to do things in XCode just makes your learning much easier than looking at the photos in the book. On the other hand, learning is more shallow and it is much easier to just follow the videos and learn only the basics without bigger picture about iOS SDK. I somehow missed more challenging exercises (even though I don't like to do them ;)

Length:
Course it pretty long but many videos are quite short (1-2…

How to Learn when Life is Short

I haven't really think this through but I have read really interesting articles recently and I just wanted to write for myself later.

Life is Short by Paul Graham

It's just that, learn from people older that you what is most important, and that life actually is short...

Instead of Goals or Resolutions, Try Creating Rules by Leo Babuta

In the time of New Year Resolutions, it's good to read something that is proposing different way of achieving things. Still I see much more value in habits than setting goals. But...

The Easy Way To Learn Hard Stuff by Per Harald Borgen
It's something that I read from time to time and I know it works sometimes for me. The things I learn then are really learned and something Jakub Wasilewski  has told me many times to do. Still it's uncomfortable and for some reason, I have failed to use this method more than once or twice. Writing it here so maybe I can think about it more and figure out how to do that more often.

Towards an understandin…