I am facing an issue with a service(factory) that relies on another service to fetch data. Here is a simplified version of the code:
factory('myFactory', function(anotherFactory) {
var factoryObject = {};
factoryObject.myMethod = () {
var newObjectToReturn;
// asynchronous call
anotherFactory.get(id, function(data) {
// process the data
newObjectToReturn = data;
});
return newObjectToReturn;
}
return factoryObject;
});
The problem arises due to the asynchronous nature of the call, resulting in factoryObject.myMethod() always returning undefined. This is because return newObjectToReturn is executed before the data is available. I am looking for a solution to bypass this limitation. Any suggestions?