Skip to main content

Promises in Code




To see more general explanations of promises: Promises and Deferreds - How I Stared To Like Them

At a basic level what Promises do is the same thing as passing a callback to a function.

function myFunc(callback){...}

myFunc(alert);
var promise = myFunc();
promise.then(alert);

But it is easy to get too many callbacks (if you work without promises), you need at least two, one for error and one for success. Functions need some arguments and you end up with a monster like:

myUgly(success, error, param1, param2, param3)

or move to a config object:

myBetter({success: success, error: error});


When I want to add another success function it gets uglier:

myBetter({successes: [success1, success2], error: error});


The same functionality with promises will be probably more verbose but I think this is a good thing. The code below is much more maintainable and extensible:

var promise = myPrecious({param1, param2, param3});
promise.when(success1);
promise.when(success2);
promise.when(success3);
promise.fail(error);

This two code examples may may look very similar to you but in my opinion second one is much more elegant, clean and obvious about what is happening.

It gets even better when you think about waiting for a couple of things to happen.
The easiest solution would be something like this underscore after:


var renderNotes = _.after(notes.length, render);
_.each(notes, function(note) {
  note.asyncSave({success: renderNotes});
});
What it's doing is just storing a variable in a closure and counting invocations of the function. With an underscore it's hidden behind a nice abstraction but for me it looks like a hack.

var runs = 10;
function afterAll(){
  if(runs-- > 0) return;
  //do something useful
}
ajax1(afterAll);
ajax2(afterAll);
...


Now look at similar code but with promises (from Q.js):

//notes is array of promises

Q.all(notes).when(afterAll);



jQuery

For jQuery this will look similar:

var promise1 = $.ajax("/myServerScript1");
var promise2 = $.ajax("/myServerScript2");
 
$.when(promise1, promise2).done(function(response1, response2){
  // Handle both responses
});


Code examples were not tested but some of them were copied from other sites so they may work. I've created them only for explanation purposes.



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…