title: How to get global variable to stay in angular constructor & promise, calling a func that needs the data from dif component
body: <p>Not an expert at angular far from it. But I've been looking deeply and i cant figure out why my other components that call the function types, runs before the constructor. and to solve it where do i put the &quot; echo &quot; function? everything works likes a charm except for the fact that echo is called before types. what or how do i make echo come first to run before any other function. i cant hook it up to the promise because it takes data from another component. i ran a if statement to check if the global variable exist and obviously doesn't because of the order of processes.</p>
  <pre><code>import { Injectable, OnInit, OnDestroy } from '@angular/core';
  import { HttpClient, HttpHeaders } from '@angular/common/http'
  import { Observable, of } from &quot;rxjs&quot;;
  import { Router, ActivatedRoute } from '@angular/router';
  import { Location } from '@angular/common/'
  import { DataService } from './products.service';
  import { BehaviorSubject, Subscription } from 'rxjs';
  import { map } from 'rxjs/operators';
  @Injectable({ providedIn: &quot;root&quot; })
  export class CartService implements OnInit, OnDestroy {
    public data: any = { &quot;productObjs&quot;: [] }
    public array: any;
    public datap: any;
    private sub: Subscription;
    //loop up the id if specexist remove thespec if empty remove id
    constructor(public dataservice: DataService, private http: HttpClient) {
      this.echo()
    }
    echo() {
      let headers = new HttpHeaders();
      headers.append('Content-Type', 'application/json');
      let prom = new Promise((resolve, reject) =&gt; {
        this.http.get('../assets/productCategories/products.json', { headers }).toPromise().then((data: any) =&gt; {
          console.log(data)
          var dat = this.datap
          resolve(dat)
          this.datap = data
          return dat
        }).then((dat) =&gt; { this.nextt(dat) });
      })
      return this.datap;
    }
    nextt(datap) {
      console.log(datap)
      this.datap = datap
    }
  // types is called from another component and runs before promise finishes
    types(type) {
      if (this.datap) {
        console.log(this.datap)
        let that = this
        var func = type.func
        var rtype = type.type
        var arr;
        switch (func) {
          case &quot;random&quot;:
            return that.sortByRandom()
            break;
          case &quot;category&quot;:
            return that.sortByCategory(rtype)
            break;
          default: &quot;specific&quot;
            return that.sortBySpecific(rtype)
        }
        console.log(this.array)
        console.log(this.array)
        console.log(this.array)
        console.log(this.datap)
        return this.array;
      }
    }
    getArray() {
      if (this.datap) {
        console.log(this.array)
        return this.array
      }
    }
    sortBySpecific(specific) {
      let that = this
      console.log(that.datap)
      var type = that.datap.product.filter(function(objj) {
        return objj.type === specific;
      })
      return type
    }
    sortByCategory(category) {
      let that = this
      var type = this.datap.product.filter(function(objj) {
        return objj.productCategory === category;
      })
      return type
    }
    sortByRandom() {
      var cats = []
      var picked = []
      var sproducts;
      let that = this
      this.datap.productObjs.forEach((obj) =&gt; {
        var randomnum2 = Math.floor(Math.random() * this.datap.productObjs.length)
        cats.push(obj.category)
      })
      var randomnum = Math.floor(Math.random() * cats.length)
      var selectedCats = this.datap.product.filter(function(objj) {
        return objj.productCategory === cats[randomnum];
      });
      sproducts = selectedCats
      var x = sproducts[Math.floor(Math.random() * sproducts.length)]
      picked.push(x)
      that.array = picked
      return picked
    }
    addToCart(ps, pobj) {
      var checkarray = this.data.productObjs.filter(function(obj) {
        return obj.productSpec === ps;
      });
      console.log(checkarray)
      if (checkarray.length &lt;= 0) {
        this.data.productObjs.push(pobj)
      }
    }
    getItems() {
      return this.data.productObjs
    }
    clearCart() {
      this.data.productObjs = []
    }
    clearProduct(objspec) {
      var newarray = this.data.productObjs.filter(function(obj) {
        return obj.productSpec !== objspec;
      });
      this.data.productObjs = newarray;
    }
    changeInventory() {
      //update pricing from inputs
    }
    checkout() {
      this.http.post('http://localhost:4201/api', this.data).subscribe((res) =&gt; {
        console.log(res)
        var json = res
        if (json['bool'] === &quot;false&quot;) {
          //cant check out
          // this checks inventory also.
          //pop up error problem with pricing.
        }
        if (json['bool'] === &quot;true&quot;) {
          //can check out
          //neeeds to request paypal to send productinfo and once payment response is succeded send valid, and delete from database.
        }
      })
    }
    ngOnInit() {
    }
    ngOnDestroy() {
      this.sub.unsubscribe();
      console.log(this.sub)
      console.log(this.datap)
    }
  }
  </code></pre>