Мне просто нужно простое угловое приложение, чтобы вызвать внешний API (запрос на получение) и измерить, как долго приложение должно получать данные.
Мой код выглядел так:
measureData(): {
this.t0 = performance.now();
this.errors = '';
this.getHello().subscribe(
(result: string) =>
{
this.text = result
console.log("test")
return this.text
},
error => {
this.errors = error.message
console.log(error.message)
return this.errors
}
)
this.t1 = performance.now();
this.test = this.t1 - this.t0;
console.log("Action took " + this.test + " milliseconds.")
this.times.push(this.test);
}
В этом случае у меня проблемы с асинхронизмом. Итак, я попробовал это:
main(){
this.measureData().subscribe(
(result: String) => {
this.measurePerformance();
}
)
}
measureData(): Observable<any> {
this.t0 = performance.now();
this.errors = '';
this.getHello().subscribe(
(result: string) =>
{
this.text = result
console.log("test")
return this.text
},
error => {
this.errors = error.message
console.log(error.message)
return this.errors
}
)
return
}
measurePerformance() {
this.t1 = performance.now();
this.test = this.t1 - this.t0;
console.log("Action took " + this.test + " milliseconds.")
this.times.push(this.test);
}
В этом случае я получаю эту ошибку:
ERROR TypeError: Cannot read property 'subscribe' of undefined