-1

I have a service that when Angular boots fetches documents and makes the available in a Map<string, Document> .

The HttpClient is used to fetch the documents.

Is it possible to delay the construction of the service until all the documents have been received.

In other words how can we make sure that when we constructor inject the service like this:

constructor(service:Service)

All the asynchronous operations that the service has initiated have completed such that all the documents are available?

I think this is an anti pattern and we should use RxJS, but figured I'd check.

Ole
  • 41,793
  • 59
  • 191
  • 359
  • 1
    you can use an APP_INTIALIZER if you HAVE to have the data prior to anything else (like a remotely loaded config the app widely relies on) but if it CAN wait, just use rxjs and subjects and all – bryan60 Dec 05 '19 at 02:16
  • Yeah I think I will probably just use Subjects. I can just emit the data when `Promise.all` completes. – Ole Dec 05 '19 at 02:45
  • If you’re using the http client, you should probably be using forkJoin instead of converting to promises – bryan60 Dec 05 '19 at 03:01

1 Answers1

1

Maybe you can use injector. constructor(private injector: Injector)

And once your document is available,

if(document){ this.serviceInstance = this.injector.get(Service) // complete other operations in this block }

SO link

Anand Bhushan
  • 765
  • 8
  • 18