I'm writing a response intercepter for an API call with ngResource like this:
var Thing = $resource('/api/things/:id', {id: '@id'}, {
queryAll: {
method: 'GET',
interceptor: {
response: function(response) {
var instance = response.resource;
doAsyncStuffHere(instance.data);
return instance;
}
}
}
});
The interceptor function should enrich the data from the response but returns the unmodified instance before the async stuff is done (as expected, since it's async...).
Is there a good way, to enrich the data asynchronously and make the return statement "wait" until it's done? Callback? Promise?
After searching the right question, i've found an article who solved my issue:
From: http://www.webdeveasy.com/interceptors-in-angularjs-and-useful-examples/
And here is what my code now looks like and working like a charm.