How do you determine when an angularjs $service has finished its work?

I am using the $location service inside my controller to enable deep linking. My controller does something like this:

$location.path("/"+param);
$("title").text(param);

It's critical that the second line finishes executing after the first. Otherwise the history item inside the browser will show the new title for the old url instead of the old one. Google.com currently has this error, try a search from the homepage, and then another search, and then hold down the back button in your browser and press on the most recent history item, you'll see what I mean.

One way to fix this is:

$location.path("/"+param);
setTimeout(function() {
    $("title").text(param);
}, 1);

...but that just doesn't sit too well with me. So my question is how do I defer the second line's execution robustly once the $location singleton has finished its own business?

AngularJS: Using $location

Note that the setters don't update window.location immediately. Instead, $location service is aware of the scope life-cycle and coalesces multiple $location mutations into one "commit" to the window.location object during the scope $digest phase.

I think you are looking for a methodology that fits async services in general (??). It seems to me that you would want to look at the promise/deferred implementation see http://docs.angularjs.org/api/ng.$q

"At first it might not be obvious why this extra complexity is worth the trouble. The payoff comes in the way of guarantees that promise and deferred apis make."

hope this helps

--dan