title: How to get data from a service in ngOnInit Angular
body: <p>I've a problem trying to get data in ngOnInit from a http service. I need to set my data in a var when the component is fired, so I can use it in another functions.</p>
  
  <p>I've achieved this by doing async ngOnInit, and receiving the response from a Promise function, but I dont think its a good practice ? How can I improve it?</p>
  
  <p>Here is my code:</p>
  
  <p><strong>COMPONENT.TS</strong></p>
  
  <pre><code>
  
       async ngOnInit() {
          this.username = localStorage.getItem('username');
          this.segment.value = 'FUNCIONAL';
          this.categoria = this.segment.value;
          this.listadoClases = await this.claseServicio.obtenerListadoClases(this.categoria); // **Need to set this variable at onInit**
  
  
  </code></pre>
  
  <p><strong>SERVICE.TS</strong></p>
  
  <pre><code>
      getClases(actividad) {
          return this.http.get(`https://anders-test.herokuapp.com/clases/obtenerPorCategoria/${actividad}`);
        }
  
  
        obtenerListadoClases(categoria) {
          return new Promise(resolve =&gt; {
            this.getClases(categoria).subscribe(data =&gt; {
              if (data) {
                resolve(data)
              }
            })
          })
        }
  
  </code></pre>
  
  <p>Im doing this because Im trying to learn/understand async/await. I want to level up my code a little bit.. But Im not getting the idea after reading a lot..</p>
  
  <p>Thanks in advance guys!</p>