New blog domain: kula.blog

It's still work in progress but new posts will be published on  https://kula.blog/ Thanks to 11ty base blog RSS is working from the start so it should be easy to add to your reader if you still use any :) If now then I hope you can sign up to the newsletter where I'll be publishing new posts and interesting articles to it from time to time. Please use  kula.blog  from now on.

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

How to use NPM packages from private repositories on bitbucket

How to simulate slow connection (developer proxy in Node.js)