How to pass content from service to another service where something happens to that content and it is then passed to a controller?

I am successfully parsing content in a service and passing that promise to another service. I would like to be able to do something to the resolved value of that promise and then store it in a way that it can be referenced from controllers that are injected with that service.

I'm not quite sure how to make this happen. Here is my attempt:

angular.module('test').service("service1", function($q) {
          function doSomething(something) {    
               //parses some data
               return deferred.promise;
           }
      }

      return {
          function1: function1
      }
});

 angular.module('test').service("service2", ['service1', function(service1) {
     function function1() {
         service1.doSomething("test").then(function (result) {
             var alteredResult = result + "Altered"; //I need to store this value so I can reference it from controllers that are injected with this service
         });
         return alteredResult;
     }
 }]);


angular.module('test').controller('testController', ['$scope', 'service2', function ($scope, service2) {

      $scope.test = service2.function1();

}]);

I am able to successfully pass the original promise from service to service to controller pass the promise and do the .then in the controller along with my alterations, but I would like to keep this logic in service2. I would like to be able to store this value in service2 so I can access it when I inject it into multiple controllers.

How can I get the altered content to be stored in my second service? Do I need to create a new promise?

Thank you very much for your time. Let me know if you need any additional info from me or if I am being unclear.

Here is a previous post where someone helped me get the promise successfully passed from service to service to controller, so that it could be accessed and altered from the controller that may be useful.


ANSWERS:


If you want to share the same value with multiple controllers you can store the altered value in an object in service2 and expose that to the controllers.

angular.module('test').service("service2", ['service1',
  function(service1) {
    // we're exposing an object with a null property
    var myResult = {
      alteredResult: null
    };

    function function1() {
      service1.doSomething("test").then(function(result) {
        // set the alteredResult property to the actual result
        myResult.alteredResult = result + "Altered";
      });

    }

    return {
      function1: function1,
      myResult: myResult
    };
  }
]);

In the controller you can then:

angular.module('test').controller('testController', ['$scope', 'service2',
  function($scope, service2) {

    // get a reference the server's exposed object, it's alteredResult property will be filled in later.
    $scope.myservice2Result = service2.myResult;
    $scope.test = service2.function1();


  }
]);

If you need to return the result of the call in service2 only to the caller then you can create a new promise.



 MORE:


 ? How do I return the response from an asynchronous call?
 ? Use of Promise in method returning Observable cause error
 ? Use of Promise in method returning Observable cause error
 ? Use of Promise in method returning Observable cause error
 ? Angular2 Observable - how to wait for all function calls in a loop to end before proceeding?
 ? Can't call methods inside catch or then of a Promise call
 ? Angular2 synchronous promise
 ? Ionic 2/Angular 2 promise returning observable
 ? How to return value of promise using angular 2
 ? Concat Observables