title: How to return data from subscriber next method in angular 2
body: <p>My project in angular consist of lot calls to api, in which most of the code accomplish the common task like saving the response to a variable. I need to optimize the code for performance. So I thought of doing something like below.</p>
  
  <pre><code>@Injectable()
  export class UtilsService {
  
    constructor( private _api: ApirequestService ) { }
  
    getItems(url, data, succFunc?: (data) =&gt; void, errFunc?: (err) =&gt; void, compFunc?: () =&gt; void) {
      return this._api.fetchApiData(url,data).subscribe(
        data =&gt; {
          if( succFunc != undefined &amp;&amp; typeof succFunc == 'function' ) succFunc(data);
        },
        error =&gt; {
          if( errFunc != undefined &amp;&amp; typeof errFunc == 'function' ) errFunc(error);
        },
        () =&gt; {
          if( compFunc != undefined &amp;&amp; typeof compFunc == 'function' ) compFunc();
        }
      )
    }
  </code></pre>
  
  <p>I created a service and injected logic outside as a parameter of the function. Is there better way to accomplish the same task with better logic. Thanks in Advance.</p>