Skip to main content

$.Deferred and Promises

I've been beating my head against $.Deferred and Promises for awhile now, ever since working on Rebecca Murphey's JS Assessment. I think I managed to stumble through the assessment, but I didn't really understand what I was doing. I just knew that Deferred/Promises were used for async management... I think.

Since then, I didn't really think to bother using Deferred/Promise until working on a project for work today. I wanted to do something like the following:

Get a session key via ajax
When successful, use the key to instantiate the client
Get a list of objects according to some criteria (this would be done asynchronously, via an API)
From that list of objects, load some kind of media 

All the articles I read today used ajax/xhr as a prime example, which was helpful for my ajax call. However, I was pretty clueless how to Deferred/Promise-ize the call that asynchronously loads the objects through the API. After some trial and error, here's what I came up with:


var step1 = $.ajax(...);
step1.done(... instantiate client ...);
step1.fail(... failed ...);

var step2 = step1.then(
  function(data) {
    return getListAsync();
  },
  function(err) {}
  );

var step3 = step2.then(
  function(data) {
    return loadData(data);
  },
  function(err) {}
  );

function getListAync() {
  var def = $.Deferred();
  def.done(function(result) {
    // do some stuff
    return result.objects;
  });

  var callback = function(args) {
    def.resolve(args);
  };

  theAsyncAPI.someCall(callback, arg1, arg2, arg3);
  return def.promise();
}
I have no idea if there is a better way to do this, so hopefully as I keep working on this, I'll figure it out better. At least it doesn't seem so confusing now. :)

Comments

Popular posts from this blog

The end of summer

It's been a wild year. Gains and losses. COVID-19 and the shelter in place. Lately, fires. Today was a very dark day, literally. For about half the daylight hours, there was so much ash in the air that it looked like it was close to sunset, but it was definitely not anywhere near as beautiful. Red sun following us everywhere. Bubonic plague found on fleas in Lake Tahoe. The list goes on and on. Everyone is tired. Exhausted. Burnt out. There is no respite, just a new normal that seems like an even steeper climb than before. Sleep gives way to another day with no refreshment. Yes, that's how things seem. But there have also been bright moments. Time spent with family, especially with the little one. Growing and nurturing plants, building things, trying new recipes, going to the store to shop for dinner, reading books, coloring with crayons... We grow closer together, weaving together a blanket of love and memories that we wrap ourselves in.

Resizing /root partition in Debian 7 (Wheezy)

I've usually been afraid to do anything drastic with the underlying filesystem because of how many times I've messed up the filesystem to begin with, and also because of how much time it takes to reinstall everything. This time, I decided that it was ok to go ahead and take a little risk here. My Linux machine is an old X61s, and its purpose is for me to experiment and learn more about Linux. I started getting a message that I was running low on space on root. Sure enough, the 300MB was all used up. I was hoping that GPartEd could help me with resizing the partitions. I couldn't see the partitions because it was all under an lvm. My plan was to shrink /opt slightly, then grow root. Thankfully, the formula isn't too bad, but there are a few steps. The first link I resorted to was this one: http://blog.shadypixel.com/how-to-shrink-an-lvm-volume-safely/. I didn't reboot to resize /opt; I just resized it after unmounting it. Unmount /opt:   # umount /dev/chrono...