angular/angular.js — pull request #3195 by ctrahey (2013-07-11 00:21:35+00:00)
Title: Allow service factory functions to return promises

This change to $injector.invoke allows user-defined services to return promises from their factory functions.

Any guidance is most welcome. Here's the motivation:

I have a service that needs to do an asynchronous task before it even knows what methods it will offer (think of it like a proxy for a WSDL--defined service or something like that). I would like this to be highly re-useable (i.e. I mean "proxy" in the sense that I am not the author of the "logical" service that the users will be consuming), so I do no want users to work with their injected dependencies in a new way (e.g. promises).

Enter this change-set.

The main idea is that when the injector is asked to invoke something, it should "wait" (via it's own promise) until all of the invocation's dependencies have been resolved before performing it so that user-land code always gets resolved services, not promises. This, of course, will allow us to write service factory functions that return promises, and thus have "asynchronously constructed" services. This opens the door to the use of some advanced service-level architectures.

I'm happy to work on this further (e.g. provide tests), but I would love some feedback on the bigger picture here.

Thanks!