-1

I am wondering if I can get help on some behavior I'm unsure about. In my code below, clients.forEach() gives an error, I assume because it executes at the same time as the forkJoin(). So clients is empty. Is there a way to have the next code "wait" for the forkJoin() function to complete first, without having to put the clients.forEach() inside the subscription() function. The clients.forEach() code is actually quite long and messy.

forkJoin({
      xclients: this.clients$,
      xkycMetrics: this.clientsBySHID$
    }).subscribe(({xclients, xkycMetrics}) => {
      clients = xclients,
      kycMetrics = xkycMetrics
    });

      // Client Data
      clients.forEach(client => {......});
stengb
  • 15
  • 4
  • Does this answer your question? [How do I return the response from an Observable/http/async call in angular?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular) – R. Richards Sep 29 '21 at 14:56
  • Well I knew that it fires asynchronously, I was just wondering if there was an alternative to keep the subscription function so large. – stengb Sep 29 '21 at 15:33

1 Answers1

0

the only way to be sure the data is ready is by adding to that subscribe function. you can always separate the logic out into its own function and make a call to it:

}).subscribe(({xclients, xkycMetrics}) => {
  clients = xclients,
  kycMetrics = xkycMetrics
  this.mapClients(clients)
});

mapClients(clients: type) {
   clients.forEach(client => {......});
}
cattourist
  • 209
  • 2
  • 5