title: Angular and Observale: how to avoid multiple requests to API within a given time
body: <p>I have something like this in an Angular 4 app (for the sake of the example, I have removed code)</p>
  
  <pre><code>@Injectable()
  export class SomeService {
  
    constructor(
      private http: Http
      ) {
    }
  
    get(id: number) {
      return this.http.get('http://somedomain/somemodel/${id}.json');
    }
  
  }
  </code></pre>
  
  <p>this is used by some components to make API calls.</p>
  
  <pre><code>constructor(private someService: SomeService) {}
  ...
  someMethod() {
    // code here...
    this.someService.get(2).subscribe( someHandlerFunction );
  }
  
  someOtherMethod() {
    // more code here...
    this.someService.get(2).subscribe( someHandlerFunction );
  }
  </code></pre>
  
  <p>the problem is that I don't know when someMethod() and someOtherMethod() will be called. Sometimes both of them may be called and then, my API will be called twice. What I am trying to find is if there any way to change my Service to do this request only after an X amount of time. I tried to use debounce:</p>
  
  <pre><code>get(id: number) {
    return this.http.get(`http://somedomain/somemodel/${id}.json`).debounceTime(10000);
  }
  </code></pre>
  
  <p>expecting that (in this case) this HTTP get request will only be repeated after 10 seconds, if the request is made within this 10 seconds, the request won't repeat but the observable emits the last value. But this didn't work.
  Any tips?</p>
  
  <p>PS: I know I could control this using some kind of flag, but I can't do this as long I this does not scale very good. I have multiple services with a lot of HTTP requests.</p>
  
  <p>Any thoughts about this?</p>