title: How to Return From Observable in TypeScript Method with Return Type
body: <p>I have a Google geocode service in TypeScript.  I need this method to return a "GoogleMap" class, after it fetches the lat/long for a given address.</p>
  
  <p>I created a TypeScript method that returns a "GoogleMap" type.  But, I'm getting a </p>
  
  <blockquote>
    <p>function that is neither void nor any must return a value...</p>
  </blockquote>
  
  <p>Here's my method:</p>
  
  <pre><code>getLatLongFromAddress(streetAddress: string): GoogleMap {
  
      this.geoCodeURL = GOOGLE_GEOCODE_BASE_URL + streetAddress +
          "&amp;key=" + GOOGLE_MAPS_API_KEY;
  
      this.googleMap = new GoogleMap();
  
      this.httpService
          .get(this.geoCodeURL)
          .subscribe((data) =&gt; {
              this.googleMap.lat = data.results.geometry.location.lat;
              this.googleMap.long = data.results.geometry.location.lng;
  
              return this.googleMap;
          },
          (error) =&gt; {
              console.error(this.geoCodeURL + ".  " + error);
  
              return Observable.throw("System encountered an error: " + error);
          },
          () =&gt; {
              console.info("ok: " + this.geoCodeURL);
  
              return this.googleMap;
          });
  }
  </code></pre>
  
  <p>I can understand the http call will be async and the flow ought to continue to the bottom of the method, possibly before the response returns data.  To return a "GoogleMap", do I need to await this Observable?  How do I go about doing this?  </p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE: 4/21/16</strong>
  I finally stumbled on an approach that I'm finding some satisfaction.  I know there's a number of posts from developers begging for a "real" service.  They want to pass a value to the service and get an object back.  Instead, many of the answers don't fully solve the problem.  The simplistic answer usually includes a subscribe() on the caller's side.  The down-side to this pattern, not usually mentioned, is that you're having to <em>map</em> the raw data retrieved in the service in the caller's callback.  It might be ok, if you only called this service from this one location in your code.  But, ordinarily, you'll be calling the service from different places in your code.  So, everytime, you'll map that object again and again in your caller's callback.  What if you add a field to the object?  Now, you have to hunt for all the callers in your code to update the mapping.  </p>
  
  <p>So, here's my approach.  We can't get away from subscribe(), and we don't want to.  In that vein, our service will return an <code>Observable&lt;T&gt;</code> with an observer that has our precious cargo.  From the caller, we'll initialize a variable, <code>Observable&lt;T&gt;</code>, and it will get the service's <code>Observable&lt;T&gt;</code>.   Next, we'll subscribe to this object.  Finally, you get your "T"! from your service.  </p>
  
  <p>Take my example, now modified.  Take note of the changes.  First, our geocoding service:</p>
  
  <pre><code>getLatLongFromAddress(streetAddress: string): Observable&lt;GoogleMap&gt; {
    ...
    return Observable.create(observer =&gt; {
        this.httpService
            .get(this.geoCodeURL)
            .subscribe((data) =&gt; {
                ...
                observer.next(this.googleMap);
                observer.complete();
            }
  </code></pre>
  
  <p>So, we're wrapping the googleMap object inside the "observer".  Let's look at the caller, now:</p>
  
  <p>Add this property: </p>
  
  <pre><code>private _gMapObservable: Observable&lt;GoogleMap&gt;;
  </code></pre>
  
  <p>Caller:</p>
  
  <pre><code>getLatLongs(streetAddress: string) {
       this._gMapObservable = this.geoService.getLatLongFromAddress(this.streetAddr);
  
       this._gMapObservable.subscribe((data)=&gt;{
            this.googleMap = data;
       });
  }
  </code></pre>
  
  <p>If you notice, there's no mapping in the caller!  you just get your object back.  All the complex mapping logic is done in the service in one place.  So code maintainability is enhanced.  Hope this helps.</p>