Skip to content

Date archive for: September 2015

Angular $resource has a $promise inside

I started working with Angular back in 1.0.6 – and boy was it painful trying to learn promises with what Angular had going on. Here is some (almost) real code I wrote:

This is an antipattern called “using Deferred” – it’s an antipattern nowadays, because it is almost always better to just return a promise than to create one. But, as far as I know, I didn’t have a choice, since $ would always return a $resource, but you couldn’t get to the private $promise locked inside. Thank goodness with 1.2 $resource has opened up access to its internal promise. Now, all that code can look like this:

Obviously this is better. It’s not easy to understand promises when you first encounter them, but as promise libraries mature, the learning curve is starting to ease.