Hello javascript, I promise to call back

I went to a job interview the other day for a javascript developer (though it was advertised as a UI developer). It was pretty tough interview, coding an MVC javascript object with Kendo UI and answering some tough questions about javascript language patterns. The toughest being: explain the difference between a defer, callback and promise (and there might have been another one…) I got sidetracked and never really answered this which has been kinda bugging me. I’m not sure I actually know the answer—this is a depth of javascript programming I haven’t explored much (and certainly don’t get an opportunity to explore in my ‘UI specialist’ job) but I’ll have a stab. Then I’ll look it up.

First up, I think they are all asynchronous techniques. Async is all the rage, I started hearing lots about it when news about node.js started hitting the blogs. There was a bit of it back when AJAX was a hot buzzword, but it was limited to handling of HTTP responses then (which pretty much had to be async—delaying your whole program to fetch a resource over the net would be too slow). Now we’re using async all over the place!

My layman’s understanding is that a good use of async removes bottlenecks. Say you need to read a file or fetch data from a server or spin off a complex computation? Have that run separately and let your program keep running. You’ll pick that up when it’s ready. In synchronous coding, your code would be stuck waiting for everything to execute in order. That’s my understand of async coding. And I think callbacks, defer and promises are async patterns.

Callbacks I know about. A function that ‘calls back’ takes another function as an argument. It will call that function at the appropriate time (when it finishes what it was doing)—for example: when a HTTP response is received, when an event occurs, when an animation completes. jQuery has plenty of callbacks.

Defer I’m not sure about. I know there is a defer attribute in html for script elements, which indicates the browser can delay executing the script. I have a feeling there might be another concept called defer which is a programming pattern though.

I have only recently encountered promises on the Javascript weekly mailing list (which is excellent) and haven’t read the articles about them. I think they are similar to callbacks, if not a specific type of callback. The name itself is also a clue, I think the ‘promise’ relates to how the function calls back. I even think it might be a callback API. And I think at this point I’ve missed it and need to do some research 😉

I’ll be back to update this article after that!

[Update: ok, it looks like promises are a pattern for callbacks which make the code easier to read. A promise returns an object immediately. That object has methods to register callbacks. ‘Then’ and ‘When’ seem to be conventions for the method names. That reminds me of behaviour-driven development, but has nothing to do with it. Well, maybe I have seen the promise pattern in some BDD code. Aha! Chainable callbacks. It seems a promise is an alternative to throwing exceptions too, nifty! I’ve added some references to the end of this post.]

[Update 2: I can’t find much extra detail on defer, unless they were asking me about $.Deferred in jQuery, which looks to be a sort of promise factory.]

I really enjoy coding in javascript but I’ve never been a full-time developer (nor wanted to be). I don’t even know if I’d even qualify as a paradev! To my workmates it probably looks like I code a lot, but there’s no pairing with another javascript coder and challenging ourselves to explore the latest best practices in javascript. It would be pretty awesome to do that every day and really push those limits—but there are lots of other interesting ideas to explore! Another article I read this week was contentiously titled Why UX Designers Need To Become Project Managers (to which I say—cool! But it better be PRINCE2 and it had better embrace product-based planning—absolutely no PINO!)

I could do advanced javascript on my own time, and I do a little—when I’m not busy with other hobbies—playing bass, gaming and family. I’m happy with the balance I have going. Also, sometimes I like to play with graphics and HTML semantics, and only get into scripting when it ‘progressively enhances’ my already useful code 😉

References

  1. Asynchronous Programming in JavaScript with “Promises”
  2. What’s so great about JavaScript Promises?
  3. You’re Missing the Point of Promises (this article is great!)
  4. ASYNCH JS: THE POWER OF $.DEFERRED
Advertisement