I'm new to Javascript and AngularJS and this one makes me scratch my head :/
Precondition
- A REST Service providing my data from the backend
- AngularJS 1.2.21 and Restangular 1.4.0
- An AngularJS controller, that shall ask the service for a spiced up version of the provided
What I have
This is the method in question:
service.getSlices = function() {
Restangular.all('entries').getList().then(function(entries) {
//some rather complex modification of the backend data go here
//...
return resultOfModification; //this is what should be returned for getSlices();
})
//I want the resultOfModification to be returned here
};
The question
Bascially I would like to wait in getSlices()
until the promise is resolved in order to return my resultOfModification
only when it actually is calculated.
Additional scenario
I could also image to return a promise from getSlices()
which would then provide the resultOfModification
. However I fear I do not understand this well enough and / or am too frustrated / tired meanwhile.
Answers and any suggestions are welcome, especially pointers to good reading material. Thanks
Restangular
is async (getSlices
is left before thethen
callback is called). That's whyPromise
is used. So the correct way would be to return the Promise and do:service.getSlices().then(function(resultOfModification) { });
?getSlices()
return a promise that will contain myresultOfModification
?