How do I wait for a response from the server?

  • 0
    There is a class with DI dependencies:

    export class DocumentsSettings implements Settings {
        constructor(
            public filtersRepository: DocumentsFiltersRepository,
            public sortingRepository: DocumentsSortingRepository,
            private http: HttpClient,
        ) {
            this.load();
        }
    
        private load(): void {
            this.http
                .get(`${this.url}/properties`)
                .pipe(map((response: string) => JSON.parse(response)))
                .subscribe((response: ResponseSettings) => {
                         this.filtersRepository.modifyDefault();
               });
    }


    The problem is that the `load ()` method returns data asynchronously faster than in the class: DocumentsFiltersRepository

    export class DocumentsFiltersRepository extends FiltersRepository {
        constructor(private documentsReferencies: DocumentsReferencies) {
            super();
    
            this.reestrTypes.subscribe((collection: any[]) => {
                this.setDefault([
                    new Filter({
                        title: 'По статусу',
                        type: FilterType.DocumentsStatus,
                        collection: documentsStatusList,
                    }),
                    new Filter({
                        title: 'Реестр',
                        type: FilterType.DocumentsReestr,
                        collection: collection,
                    }),
                ]);
            });
        }
    
        public get reestrTypes(): Observable<any> {
            return this.documentsReferencies.reestrtypes;
        }
    }


    I need to make sure that before calling the method: this.filtersRepository.modifyDefault (); , the method, depending on the dependency, would have already worked and returned the data:

    this.reestrTypes.subscribe((collection: any[]) => {}


    How to achieve this?
    JavaScript Anonymous, May 11, 2019

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!