ожидание асинхронных операций в угловых - PullRequest
0 голосов
/ 27 апреля 2018

В следующем фрагменте кода, как я могу дождаться завершения асинхронных операций и затем выполнить цикл?

ngOnInit() {
       this.userService.query()
        .subscribe((res: HttpResponse<User[]>) => { this.users = res.body; }, (res: HttpErrorResponse) => this.onError(res.message));

    this.principal.identity().then((account) => {
        this.currentAccount = account;

    });

    //my loop 
    for ( const user of this.users){
        if (user.login === this.currentAccount.login){
            this.currentUserId = user.id;
        }
    }


}

1 Ответ

0 голосов
/ 27 апреля 2018

Поменяйте обещание на Наблюдаемое или Наблюдаемое на обещание, разрешите их все, затем выполните цикл.

const c1 = this.userService.query().toPromise();
const c2 = this.principal.identity();
Promise.all([c1, c2]).then(([r1, r2]) => {
  for (...)
})

const c1 = this.userService.query();
const c2 = Observable.fromPromise(this.principal.identity());
Observable.forkJoin([c1, c2]).subscribe([r1, r2] => {
  for(...)
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...