code snippet

Async patterns: Promises

Given our understanding of what async is, how the callback pattern manages async, and some of the deficiencies of the callback pattern, let’s dive into another, newer pattern — promises.

Promises are clearly exciting the JS community. As someone who never really knew JavaScript before promises, I have only an academic understanding of the magnitude of this shift. In fact, I think I was exposed to management of async first with callbacks to make promises seem just that much more awesome (like building a node server before meeting Express). Understanding both is important to a deeper understanding of each, individually.

As with callbacks, for a new developer, it can be difficult to sift through the massive amount of information available online. For the benefit of anyone else newly exposed to promises, in this post I’ve attempted to cull the resources and posts that I have found most enlightening through my own learning.

What are promises all about?

Before diving into code, what are promises, conceptually? There are many out there, but I found Kyle Simpson’s ‘cheeseburger’ metaphor to be illuminating. If you prefer to hear an explanation, watch the video embedded below (15:20 mark). If you’d rather read, check it out in ‘You Don’t Know JS‘.

Getting our feet wet

Jake Archibald’s JavaScript Promises: There and back again is a great tiered tutorial — useful for all levels. It addresses the whys of promises, simple terminology, the varying implementations of promises, and an extended code example to introduce promises in practice.

Jake Archibald’s JavaScript Promises: There and back again

Promises and callbacks: key differences

James Knelson’s Introduction to ES6 Promises – The Four Functions You Need To Avoid Callback Hell provides a succinct distillation of some core differences between promises and callbacks — I think this is helpful to pull everything together more clearly after already being acquainted with both.

James Knelson’s Introduction to ES6 Promises – The Four Functions You Need To Avoid Callback Hell

Extra snippet

Recently a friend and I pulled together a snippet that goes over constructing a new promise, using a promise to wrap another async action, and a direct comparison of a callback example and its promise alternative.

I hope this was helpful in narrowing some of the available content out there. Please leave a comment if you see any needed correction — or if there’s a resource out there that was really illuminating for you.


Leave a Reply

Your email address will not be published. Required fields are marked *