How to pause a function until a promise is returned?
<p>In my service, I am using <code>http</code> to get some data then return it as a promise. </p>

<p>I have a function like this</p>

<pre><code>onClick(ID:number){
  this.myService.getSomeData(ID).then(response =&gt; this.myObject=response);
  //do something with myObject
}
</code></pre>

<p>However, I need to wait for the service to return the data before I can continue. I cannot call the service earlier because it depends on which option the user clicked. </p>

<p>I was thinking I could wrap it in <code>setTimeout</code></p>

<pre><code>onClick(ID:number){
  setTimeout(() =&gt; {
    this.myService.getSomeData(ID).then(response =&gt; this.myObject=response);
  }, 1000);
  //do something with myObject
}
</code></pre>

<p>but one second is just a guess and I might be wasting time or not have enough time. Is there a way to pause this function until that service returns information, ideally which will timeout after about five seconds (and if this happens, have it execute my handling code)? During this time, the user should not be able to do anything. </p>