Comment

Tomas Lycken

It seems that your main motivation is to avoid having one method return different types of promises (or data) depending on what happens, as in your example with an AJAX request the first time and loading from some cache on subsequent requests - is that correct?

If so, there's a much easier way to accomplish this: just chain then-calls on your promises, and reshape the data, until the client code can use the output consistently. For example, consider the following service method which takes an url and either gets the results from a server, or loads them from cache:

function loadData(url) {
    var deferred = $q.defer();
    if (isInCache(url)) {
        deferred.resolve(getFromCache(url));
    } else {
        $http.get(url).success(function(data) { deferred.resolve(data); });
    }
    return deferred.promise;
}

This gets even easier if your cache service is promise-aware:

function loadData(url) {
    if (isInCache(url)) {
        return getFromCache(url); // returns a then-able promise
    } else {
        return $http.get(url).success(function (data) { return data; }); // also a then-able promise
    }
}

My point is, if you build your services to have a consistent API, you don't need to hack around with proxies or wrappers around $http at all.

Replies

Peter Bengtsson

I love it!

I actually used this in another project since after I published this blog post :)