K
K
Konstantin2020-11-19 02:50:28
JavaScript
Konstantin, 2020-11-19 02:50:28

How to wait for a response from the server?

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 asynchronously returns data 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 dependency method would have already completed and returned the data:

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


How to achieve this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question