Re-learning asynchronous programming

This time with JavaScript Promises

Pelle Almquist
2 min readJun 5, 2014

In the recent years there has been a lot of talk on how to write asynchronous code without getting to a code state that is unmanageable.

I’ve previously followed it from Python’s point of view where the biggest concepts have been Twisted’s Deferred object, Gevent’s monkey patching, and lately the tuilp/asyncio PEP (meaning Python will ship with built in async support).

But lately I’ve started following the discussion from a JavaScript point of view where the discussion is all about Promises.

My previous experience is mostly with Deferreds and although Promises look similar enough (they are also objects that deal with the callback chain of asynchronous calls) they do differ (here is a great SO post about the differences).

So, its time to re-learn asynchronous programming (which is actually something I need to do quite often as my knowledge in it improves and as the landscape shifts) and this time the focus is on Promises.

There are plenty of resources on JavaScript and Promises, these being my favorites so far:

Once you get a grasp of Promises and want to take it to the next level I think you should take a look at the supporting libraries Q and when.

So far, I’m liking the thenable notation of Promises and I love Q’s `.done()` feature that you can write .done() and the Promise will be closed for further callbacks and will report all errors (hoping this will be widespread).

I’m still waiting for an inlineCallbacks equivalent but I’m sure that it will be available as soon as JavaScript 7 is available which enables generators and the yield keyword.

(Mandatory pun) Other than that, JavaScript Promises has a lot of.. promise.

--

--

Pelle Almquist

I make code that sing ♪♫♩♬ Currently @helloclue previously @wrappcorp, @OrcGroupAB, and @SEBgroup.